1 /* Copyright 2001, 2002, 2003, 2004, 2005 by Hans Reiser, licensing governed by
8 /* cache or dir_cursors */
9 static struct kmem_cache
*d_cursor_cache
;
11 /* list of unused cursors */
12 static LIST_HEAD(cursor_cache
);
14 /* number of cursors in list of ununsed cursors */
15 static unsigned long d_cursor_unused
= 0;
17 /* spinlock protecting manipulations with dir_cursor's hash table and lists */
18 DEFINE_SPINLOCK(d_lock
);
20 static reiser4_file_fsdata
*create_fsdata(struct file
*file
);
21 static int file_is_stateless(struct file
*file
);
22 static void free_fsdata(reiser4_file_fsdata
*fsdata
);
23 static void kill_cursor(dir_cursor
*);
26 * d_cursor_shrink - shrink callback for cache of dir_cursor-s
27 * @nr: number of objects to free
30 * Shrinks d_cursor_cache. Scan LRU list of unused cursors, freeing requested
31 * number. Return number of still freeable cursors.
33 static int d_cursor_shrink(int nr
, gfp_t mask
)
41 while (!list_empty(&cursor_cache
)) {
42 scan
= list_entry(cursor_cache
.next
, dir_cursor
, alist
);
43 assert("nikita-3567", scan
->ref
== 0);
52 return d_cursor_unused
;
56 * actually, d_cursors are "priceless", because there is no way to
57 * recover information stored in them. On the other hand, we don't
58 * want to consume all kernel memory by them. As a compromise, just
59 * assign higher "seeks" value to d_cursor cache, so that it will be
60 * shrunk only if system is really tight on memory.
62 static struct shrinker d_cursor_shrinker
= {
63 .shrink
= d_cursor_shrink
,
64 .seeks
= DEFAULT_SEEKS
<< 3,
68 * reiser4_init_d_cursor - create d_cursor cache
70 * Initializes slab cache of d_cursors. It is part of reiser4 module
73 int reiser4_init_d_cursor(void)
75 d_cursor_cache
= kmem_cache_create("d_cursor", sizeof(dir_cursor
), 0,
76 SLAB_HWCACHE_ALIGN
, NULL
);
77 if (d_cursor_cache
== NULL
)
78 return RETERR(-ENOMEM
);
80 register_shrinker(&d_cursor_shrinker
);
85 * reiser4_done_d_cursor - delete d_cursor cache and d_cursor shrinker
87 * This is called on reiser4 module unloading or system shutdown.
89 void reiser4_done_d_cursor(void)
91 unregister_shrinker(&d_cursor_shrinker
);
93 destroy_reiser4_cache(&d_cursor_cache
);
96 #define D_CURSOR_TABLE_SIZE (256)
98 static inline unsigned long
99 d_cursor_hash(d_cursor_hash_table
* table
, const struct d_cursor_key
*key
)
101 assert("nikita-3555", IS_POW(D_CURSOR_TABLE_SIZE
));
102 return (key
->oid
+ key
->cid
) & (D_CURSOR_TABLE_SIZE
- 1);
105 static inline int d_cursor_eq(const struct d_cursor_key
*k1
,
106 const struct d_cursor_key
*k2
)
108 return k1
->cid
== k2
->cid
&& k1
->oid
== k2
->oid
;
112 * define functions to manipulate reiser4 super block's hash table of
115 #define KMALLOC(size) kmalloc((size), reiser4_ctx_gfp_mask_get())
116 #define KFREE(ptr, size) kfree(ptr)
117 TYPE_SAFE_HASH_DEFINE(d_cursor
,
120 key
, hash
, d_cursor_hash
, d_cursor_eq
);
125 * reiser4_init_super_d_info - initialize per-super-block d_cursor resources
126 * @super: super block to initialize
128 * Initializes per-super-block d_cursor's hash table and radix tree. It is part
131 int reiser4_init_super_d_info(struct super_block
*super
)
133 struct d_cursor_info
*p
;
135 p
= &get_super_private(super
)->d_info
;
137 INIT_RADIX_TREE(&p
->tree
, reiser4_ctx_gfp_mask_get());
138 return d_cursor_hash_init(&p
->table
, D_CURSOR_TABLE_SIZE
);
142 * reiser4_done_super_d_info - release per-super-block d_cursor resources
143 * @super: super block being umounted
145 * It is called on umount. Kills all directory cursors attached to suoer block.
147 void reiser4_done_super_d_info(struct super_block
*super
)
149 struct d_cursor_info
*d_info
;
150 dir_cursor
*cursor
, *next
;
152 d_info
= &get_super_private(super
)->d_info
;
153 for_all_in_htable(&d_info
->table
, d_cursor
, cursor
, next
)
156 BUG_ON(d_info
->tree
.rnode
!= NULL
);
157 d_cursor_hash_done(&d_info
->table
);
161 * kill_cursor - free dir_cursor and reiser4_file_fsdata attached to it
162 * @cursor: cursor to free
164 * Removes reiser4_file_fsdata attached to @cursor from readdir list of
165 * reiser4_inode, frees that reiser4_file_fsdata. Removes @cursor from from
166 * indices, hash table, list of unused cursors and frees it.
168 static void kill_cursor(dir_cursor
*cursor
)
172 assert("nikita-3566", cursor
->ref
== 0);
173 assert("nikita-3572", cursor
->fsdata
!= NULL
);
175 index
= (unsigned long)cursor
->key
.oid
;
176 list_del_init(&cursor
->fsdata
->dir
.linkage
);
177 free_fsdata(cursor
->fsdata
);
178 cursor
->fsdata
= NULL
;
180 if (list_empty_careful(&cursor
->list
))
181 /* this is last cursor for a file. Kill radix-tree entry */
182 radix_tree_delete(&cursor
->info
->tree
, index
);
187 * there are other cursors for the same oid.
191 * if radix tree point to the cursor being removed, re-target
192 * radix tree slot to the next cursor in the (non-empty as was
193 * checked above) element of the circular list of all cursors
196 slot
= radix_tree_lookup_slot(&cursor
->info
->tree
, index
);
197 assert("nikita-3571", *slot
!= NULL
);
199 *slot
= list_entry(cursor
->list
.next
, dir_cursor
, list
);
200 /* remove cursor from circular list */
201 list_del_init(&cursor
->list
);
203 /* remove cursor from the list of unused cursors */
204 list_del_init(&cursor
->alist
);
205 /* remove cursor from the hash table */
206 d_cursor_hash_remove(&cursor
->info
->table
, cursor
);
208 kmem_cache_free(d_cursor_cache
, cursor
);
212 /* possible actions that can be performed on all cursors for the given file */
215 * load all detached state: this is called when stat-data is loaded
216 * from the disk to recover information about all pending readdirs
220 * detach all state from inode, leaving it in the cache. This is called
221 * when inode is removed form the memory by memory pressure
225 * detach cursors from the inode, and free them. This is called when
232 * return d_cursor data for the file system @inode is in.
234 static inline struct d_cursor_info
*d_info(struct inode
*inode
)
236 return &get_super_private(inode
->i_sb
)->d_info
;
240 * lookup d_cursor in the per-super-block radix tree.
242 static inline dir_cursor
*lookup(struct d_cursor_info
*info
,
245 return (dir_cursor
*) radix_tree_lookup(&info
->tree
, index
);
249 * attach @cursor to the radix tree. There may be multiple cursors for the
250 * same oid, they are chained into circular list.
252 static void bind_cursor(dir_cursor
* cursor
, unsigned long index
)
256 head
= lookup(cursor
->info
, index
);
258 /* this is the first cursor for this index */
259 INIT_LIST_HEAD(&cursor
->list
);
260 radix_tree_insert(&cursor
->info
->tree
, index
, cursor
);
262 /* some cursor already exists. Chain ours */
263 list_add(&cursor
->list
, &head
->list
);
268 * detach fsdata (if detachable) from file descriptor, and put cursor on the
269 * "unused" list. Called when file descriptor is not longer in active use.
271 static void clean_fsdata(struct file
*file
)
274 reiser4_file_fsdata
*fsdata
;
276 assert("nikita-3570", file_is_stateless(file
));
278 fsdata
= (reiser4_file_fsdata
*) file
->private_data
;
279 if (fsdata
!= NULL
) {
280 cursor
= fsdata
->cursor
;
281 if (cursor
!= NULL
) {
284 if (cursor
->ref
== 0) {
285 list_add_tail(&cursor
->alist
, &cursor_cache
);
288 spin_unlock(&d_lock
);
289 file
->private_data
= NULL
;
295 * global counter used to generate "client ids". These ids are encoded into
298 static __u32 cid_counter
= 0;
299 #define CID_SHIFT (20)
300 #define CID_MASK (0xfffffull)
302 static void free_file_fsdata_nolock(struct file
*);
305 * insert_cursor - allocate file_fsdata, insert cursor to tree and hash table
310 * Allocates reiser4_file_fsdata, attaches it to @cursor, inserts cursor to
311 * reiser4 super block's hash table and radix tree.
312 add detachable readdir
315 static int insert_cursor(dir_cursor
*cursor
, struct file
*file
,
319 reiser4_file_fsdata
*fsdata
;
321 memset(cursor
, 0, sizeof *cursor
);
323 /* this is either first call to readdir, or rewind. Anyway, create new
325 fsdata
= create_fsdata(NULL
);
326 if (fsdata
!= NULL
) {
327 result
= radix_tree_preload(reiser4_ctx_gfp_mask_get());
329 struct d_cursor_info
*info
;
332 info
= d_info(inode
);
333 oid
= get_inode_oid(inode
);
334 /* cid occupies higher 12 bits of f->f_pos. Don't
335 * allow it to become negative: this confuses
337 cursor
->key
.cid
= (++cid_counter
) & 0x7ff;
338 cursor
->key
.oid
= oid
;
339 cursor
->fsdata
= fsdata
;
343 spin_lock_inode(inode
);
344 /* install cursor as @f's private_data, discarding old
345 * one if necessary */
347 if (file
->private_data
)
348 warning("", "file has fsdata already");
351 free_file_fsdata_nolock(file
);
352 file
->private_data
= fsdata
;
353 fsdata
->cursor
= cursor
;
354 spin_unlock_inode(inode
);
356 /* insert cursor into hash table */
357 d_cursor_hash_insert(&info
->table
, cursor
);
358 /* and chain it into radix-tree */
359 bind_cursor(cursor
, (unsigned long)oid
);
360 spin_unlock(&d_lock
);
361 radix_tree_preload_end();
362 file
->f_pos
= ((__u64
) cursor
->key
.cid
) << CID_SHIFT
;
365 result
= RETERR(-ENOMEM
);
370 * process_cursors - do action on each cursor attached to inode
374 * Finds all cursors of @inode in reiser4's super block radix tree of cursors
375 * and performs action specified by @act on each of cursors.
377 static void process_cursors(struct inode
*inode
, enum cursor_action act
)
381 struct list_head
*head
;
382 reiser4_context
*ctx
;
383 struct d_cursor_info
*info
;
385 /* this can be called by
387 * kswapd->...->prune_icache->..reiser4_destroy_inode
389 * without reiser4_context
391 ctx
= reiser4_init_context(inode
->i_sb
);
393 warning("vs-23", "failed to init context");
397 assert("nikita-3558", inode
!= NULL
);
399 info
= d_info(inode
);
400 oid
= get_inode_oid(inode
);
401 spin_lock_inode(inode
);
402 head
= get_readdir_list(inode
);
404 /* find any cursor for this oid: reference to it is hanging of radix
406 start
= lookup(info
, (unsigned long)oid
);
409 reiser4_file_fsdata
*fsdata
;
411 /* process circular list of cursors for this oid */
416 next
= list_entry(scan
->list
.next
, dir_cursor
, list
);
417 fsdata
= scan
->fsdata
;
418 assert("nikita-3557", fsdata
!= NULL
);
419 if (scan
->key
.oid
== oid
) {
422 list_del_init(&fsdata
->dir
.linkage
);
425 list_add(&fsdata
->dir
.linkage
, head
);
433 /* last cursor was just killed */
436 } while (scan
!= start
);
438 spin_unlock(&d_lock
);
439 /* check that we killed 'em all */
440 assert("nikita-3568",
441 ergo(act
== CURSOR_KILL
,
442 list_empty_careful(get_readdir_list(inode
))));
443 assert("nikita-3569",
444 ergo(act
== CURSOR_KILL
, lookup(info
, oid
) == NULL
));
445 spin_unlock_inode(inode
);
446 reiser4_exit_context(ctx
);
450 * reiser4_dispose_cursors - removes cursors from inode's list
451 * @inode: inode to dispose cursors of
453 * For each of cursors corresponding to @inode - removes reiser4_file_fsdata
454 * attached to cursor from inode's readdir list. This is called when inode is
455 * removed from the memory by memory pressure.
457 void reiser4_dispose_cursors(struct inode
*inode
)
459 process_cursors(inode
, CURSOR_DISPOSE
);
463 * reiser4_load_cursors - attach cursors to inode
464 * @inode: inode to load cursors to
466 * For each of cursors corresponding to @inode - attaches reiser4_file_fsdata
467 * attached to cursor to inode's readdir list. This is done when inode is
468 * loaded into memory.
470 void reiser4_load_cursors(struct inode
*inode
)
472 process_cursors(inode
, CURSOR_LOAD
);
476 * reiser4_kill_cursors - kill all inode cursors
477 * @inode: inode to kill cursors of
479 * Frees all cursors for this inode. This is called when inode is destroyed.
481 void reiser4_kill_cursors(struct inode
*inode
)
483 process_cursors(inode
, CURSOR_KILL
);
487 * file_is_stateless -
490 * true, if file descriptor @f is created by NFS server by "demand" to serve
491 * one file system operation. This means that there may be "detached state"
492 * for underlying inode.
494 static int file_is_stateless(struct file
*file
)
496 return reiser4_get_dentry_fsdata(file
->f_dentry
)->stateless
;
500 * reiser4_get_dir_fpos -
503 * Calculates ->fpos from user-supplied cookie. Normally it is dir->f_pos, but
504 * in the case of stateless directory operation (readdir-over-nfs), client id
505 * was encoded in the high bits of cookie and should me masked off.
507 loff_t
reiser4_get_dir_fpos(struct file
*dir
)
509 if (file_is_stateless(dir
))
510 return dir
->f_pos
& CID_MASK
;
516 * reiser4_attach_fsdata - try to attach fsdata
520 * Finds or creates cursor for readdir-over-nfs.
522 int reiser4_attach_fsdata(struct file
*file
, struct inode
*inode
)
529 * we are serialized by inode->i_mutex
531 if (!file_is_stateless(file
))
538 * first call to readdir (or rewind to the beginning of
541 cursor
= kmem_cache_alloc(d_cursor_cache
,
542 reiser4_ctx_gfp_mask_get());
544 result
= insert_cursor(cursor
, file
, inode
);
546 result
= RETERR(-ENOMEM
);
548 /* try to find existing cursor */
549 struct d_cursor_key key
;
551 key
.cid
= pos
>> CID_SHIFT
;
552 key
.oid
= get_inode_oid(inode
);
554 cursor
= d_cursor_hash_find(&d_info(inode
)->table
, &key
);
555 if (cursor
!= NULL
) {
556 /* cursor was found */
557 if (cursor
->ref
== 0) {
558 /* move it from unused list */
559 list_del_init(&cursor
->alist
);
564 spin_unlock(&d_lock
);
565 if (cursor
!= NULL
) {
566 spin_lock_inode(inode
);
567 assert("nikita-3556", cursor
->fsdata
->back
== NULL
);
569 free_file_fsdata_nolock(file
);
570 file
->private_data
= cursor
->fsdata
;
571 spin_unlock_inode(inode
);
578 * reiser4_detach_fsdata - ???
581 * detach fsdata, if necessary
583 void reiser4_detach_fsdata(struct file
*file
)
587 if (!file_is_stateless(file
))
590 inode
= file
->f_dentry
->d_inode
;
591 spin_lock_inode(inode
);
593 spin_unlock_inode(inode
);
596 /* slab for reiser4_dentry_fsdata */
597 static struct kmem_cache
*dentry_fsdata_cache
;
600 * reiser4_init_dentry_fsdata - create cache of dentry_fsdata
602 * Initializes slab cache of structures attached to denty->d_fsdata. It is
603 * part of reiser4 module initialization.
605 int reiser4_init_dentry_fsdata(void)
607 dentry_fsdata_cache
= kmem_cache_create("dentry_fsdata",
608 sizeof(struct reiser4_dentry_fsdata
),
611 SLAB_RECLAIM_ACCOUNT
,
613 if (dentry_fsdata_cache
== NULL
)
614 return RETERR(-ENOMEM
);
619 * reiser4_done_dentry_fsdata - delete cache of dentry_fsdata
621 * This is called on reiser4 module unloading or system shutdown.
623 void reiser4_done_dentry_fsdata(void)
625 destroy_reiser4_cache(&dentry_fsdata_cache
);
629 * reiser4_get_dentry_fsdata - get fs-specific dentry data
630 * @dentry: queried dentry
632 * Allocates if necessary and returns per-dentry data that we attach to each
635 struct reiser4_dentry_fsdata
*reiser4_get_dentry_fsdata(struct dentry
*dentry
)
637 assert("nikita-1365", dentry
!= NULL
);
639 if (dentry
->d_fsdata
== NULL
) {
640 dentry
->d_fsdata
= kmem_cache_alloc(dentry_fsdata_cache
,
641 reiser4_ctx_gfp_mask_get());
642 if (dentry
->d_fsdata
== NULL
)
643 return ERR_PTR(RETERR(-ENOMEM
));
644 memset(dentry
->d_fsdata
, 0,
645 sizeof(struct reiser4_dentry_fsdata
));
647 return dentry
->d_fsdata
;
651 * reiser4_free_dentry_fsdata - detach and free dentry_fsdata
652 * @dentry: dentry to free fsdata of
654 * Detaches and frees fs-specific dentry data
656 void reiser4_free_dentry_fsdata(struct dentry
*dentry
)
658 if (dentry
->d_fsdata
!= NULL
) {
659 kmem_cache_free(dentry_fsdata_cache
, dentry
->d_fsdata
);
660 dentry
->d_fsdata
= NULL
;
664 /* slab for reiser4_file_fsdata */
665 static struct kmem_cache
*file_fsdata_cache
;
668 * reiser4_init_file_fsdata - create cache of reiser4_file_fsdata
670 * Initializes slab cache of structures attached to file->private_data. It is
671 * part of reiser4 module initialization.
673 int reiser4_init_file_fsdata(void)
675 file_fsdata_cache
= kmem_cache_create("file_fsdata",
676 sizeof(reiser4_file_fsdata
),
679 SLAB_RECLAIM_ACCOUNT
, NULL
);
680 if (file_fsdata_cache
== NULL
)
681 return RETERR(-ENOMEM
);
686 * reiser4_done_file_fsdata - delete cache of reiser4_file_fsdata
688 * This is called on reiser4 module unloading or system shutdown.
690 void reiser4_done_file_fsdata(void)
692 destroy_reiser4_cache(&file_fsdata_cache
);
696 * create_fsdata - allocate and initialize reiser4_file_fsdata
697 * @file: what to create file_fsdata for, may be NULL
699 * Allocates and initializes reiser4_file_fsdata structure.
701 static reiser4_file_fsdata
*create_fsdata(struct file
*file
)
703 reiser4_file_fsdata
*fsdata
;
705 fsdata
= kmem_cache_alloc(file_fsdata_cache
,
706 reiser4_ctx_gfp_mask_get());
707 if (fsdata
!= NULL
) {
708 memset(fsdata
, 0, sizeof *fsdata
);
709 fsdata
->ra1
.max_window_size
= VM_MAX_READAHEAD
* 1024;
711 INIT_LIST_HEAD(&fsdata
->dir
.linkage
);
717 * free_fsdata - free reiser4_file_fsdata
718 * @fsdata: object to free
720 * Dual to create_fsdata(). Free reiser4_file_fsdata.
722 static void free_fsdata(reiser4_file_fsdata
*fsdata
)
724 BUG_ON(fsdata
== NULL
);
725 kmem_cache_free(file_fsdata_cache
, fsdata
);
729 * reiser4_get_file_fsdata - get fs-specific file data
730 * @file: queried file
732 * Returns fs-specific data of @file. If it is NULL, allocates it and attaches
735 reiser4_file_fsdata
*reiser4_get_file_fsdata(struct file
*file
)
737 assert("nikita-1603", file
!= NULL
);
739 if (file
->private_data
== NULL
) {
740 reiser4_file_fsdata
*fsdata
;
743 fsdata
= create_fsdata(file
);
745 return ERR_PTR(RETERR(-ENOMEM
));
747 inode
= file
->f_dentry
->d_inode
;
748 spin_lock_inode(inode
);
749 if (file
->private_data
== NULL
) {
750 file
->private_data
= fsdata
;
753 spin_unlock_inode(inode
);
755 /* other thread initialized ->fsdata */
756 kmem_cache_free(file_fsdata_cache
, fsdata
);
758 assert("nikita-2665", file
->private_data
!= NULL
);
759 return file
->private_data
;
763 * free_file_fsdata_nolock - detach and free reiser4_file_fsdata
766 * Detaches reiser4_file_fsdata from @file, removes reiser4_file_fsdata from
767 * readdir list, frees if it is not linked to d_cursor object.
769 static void free_file_fsdata_nolock(struct file
*file
)
771 reiser4_file_fsdata
*fsdata
;
773 assert("", spin_inode_is_locked(file
->f_dentry
->d_inode
));
774 fsdata
= file
->private_data
;
775 if (fsdata
!= NULL
) {
776 list_del_init(&fsdata
->dir
.linkage
);
777 if (fsdata
->cursor
== NULL
)
780 file
->private_data
= NULL
;
784 * reiser4_free_file_fsdata - detach from struct file and free reiser4_file_fsdata
787 * Spinlocks inode and calls free_file_fsdata_nolock to do the work.
789 void reiser4_free_file_fsdata(struct file
*file
)
791 spin_lock_inode(file
->f_dentry
->d_inode
);
792 free_file_fsdata_nolock(file
);
793 spin_unlock_inode(file
->f_dentry
->d_inode
);
798 * c-indentation-style: "K&R"