1 /* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by reiser4/README */
3 /* Inode specific operations. */
12 #include "plugin/item/item.h"
13 #include "plugin/security/perm.h"
14 #include "plugin/plugin.h"
15 #include "plugin/object.h"
22 #include <linux/fs.h> /* for struct super_block, address_space */
24 /* return reiser4 internal tree which inode belongs to */
25 /* Audited by: green(2002.06.17) */
26 reiser4_tree
*reiser4_tree_by_inode(const struct inode
*inode
/* inode queried */ )
28 assert("nikita-256", inode
!= NULL
);
29 assert("nikita-257", inode
->i_sb
!= NULL
);
30 return reiser4_get_tree(inode
->i_sb
);
33 /* return reiser4-specific inode flags */
34 static inline unsigned long *inode_flags(const struct inode
*const inode
)
36 assert("nikita-2842", inode
!= NULL
);
37 return &reiser4_inode_data(inode
)->flags
;
40 /* set reiser4-specific flag @f in @inode */
41 void reiser4_inode_set_flag(struct inode
*inode
, reiser4_file_plugin_flags f
)
43 assert("nikita-2248", inode
!= NULL
);
44 set_bit((int)f
, inode_flags(inode
));
47 /* clear reiser4-specific flag @f in @inode */
48 void reiser4_inode_clr_flag(struct inode
*inode
, reiser4_file_plugin_flags f
)
50 assert("nikita-2250", inode
!= NULL
);
51 clear_bit((int)f
, inode_flags(inode
));
54 /* true if reiser4-specific flag @f is set in @inode */
55 int reiser4_inode_get_flag(const struct inode
*inode
,
56 reiser4_file_plugin_flags f
)
58 assert("nikita-2251", inode
!= NULL
);
59 return test_bit((int)f
, inode_flags(inode
));
62 /* convert oid to inode number */
63 ino_t
oid_to_ino(oid_t oid
)
68 /* convert oid to user visible inode number */
69 ino_t
oid_to_uino(oid_t oid
)
71 /* reiser4 object is uniquely identified by oid which is 64 bit
72 quantity. Kernel in-memory inode is indexed (in the hash table) by
73 32 bit i_ino field, but this is not a problem, because there is a
74 way to further distinguish inodes with identical inode numbers
75 (find_actor supplied to iget()).
77 But user space expects unique 32 bit inode number. Obviously this
78 is impossible. Work-around is to somehow hash oid into user visible
81 oid_t max_ino
= (ino_t
) ~ 0;
83 if (REISER4_INO_IS_OID
|| (oid
<= max_ino
))
86 /* this is remotely similar to algorithm used to find next pid
87 to use for process: after wrap-around start from some
88 offset rather than from 0. Idea is that there are some long
89 living objects with which we don't want to collide.
91 return REISER4_UINO_SHIFT
+ ((oid
- max_ino
) & (max_ino
>> 1));
94 /* check that "inode" is on reiser4 file-system */
95 int is_reiser4_inode(const struct inode
*inode
/* inode queried */ )
97 return inode
!= NULL
&& is_reiser4_super(inode
->i_sb
);
100 /* Maximal length of a name that can be stored in directory @inode.
102 This is used in check during file creation and lookup. */
103 int reiser4_max_filename_len(const struct inode
*inode
/* inode queried */ )
105 assert("nikita-287", is_reiser4_inode(inode
));
106 assert("nikita-1710", inode_dir_item_plugin(inode
));
107 if (inode_dir_item_plugin(inode
)->s
.dir
.max_name_len
)
108 return inode_dir_item_plugin(inode
)->s
.dir
.max_name_len(inode
);
113 #if REISER4_USE_COLLISION_LIMIT
114 /* Maximal number of hash collisions for this directory. */
115 int max_hash_collisions(const struct inode
*dir
/* inode queried */ )
117 assert("nikita-1711", dir
!= NULL
);
118 return reiser4_inode_data(dir
)->plugin
.max_collisions
;
120 #endif /* REISER4_USE_COLLISION_LIMIT */
122 /* Install file, inode, and address_space operation on @inode, depending on
124 int setup_inode_ops(struct inode
*inode
/* inode to intialize */ ,
125 reiser4_object_create_data
* data
/* parameters to create
128 reiser4_super_info_data
*sinfo
;
132 fplug
= inode_file_plugin(inode
);
133 dplug
= inode_dir_plugin(inode
);
135 sinfo
= get_super_private(inode
->i_sb
);
137 switch (inode
->i_mode
& S_IFMT
) {
143 dev_t rdev
; /* to keep gcc happy */
145 assert("vs-46", fplug
!= NULL
);
146 /* ugly hack with rdev */
148 rdev
= inode
->i_rdev
;
153 assert("vs-42", fplug
->h
.id
== SPECIAL_FILE_PLUGIN_ID
);
154 inode
->i_op
= &file_plugins
[fplug
->h
.id
].inode_ops
;
155 /* initialize inode->i_fop and inode->i_rdev for block and char
157 init_special_inode(inode
, inode
->i_mode
, rdev
);
158 /* all address space operations are null */
159 inode
->i_mapping
->a_ops
=
160 &file_plugins
[fplug
->h
.id
].as_ops
;
164 assert("vs-46", fplug
!= NULL
);
165 assert("vs-42", fplug
->h
.id
== SYMLINK_FILE_PLUGIN_ID
);
166 inode
->i_op
= &file_plugins
[fplug
->h
.id
].inode_ops
;
168 /* all address space operations are null */
169 inode
->i_mapping
->a_ops
= &file_plugins
[fplug
->h
.id
].as_ops
;
172 assert("vs-46", dplug
!= NULL
);
173 assert("vs-43", (dplug
->h
.id
== HASHED_DIR_PLUGIN_ID
||
174 dplug
->h
.id
== SEEKABLE_HASHED_DIR_PLUGIN_ID
));
175 inode
->i_op
= &dir_plugins
[dplug
->h
.id
].inode_ops
;
176 inode
->i_fop
= &dir_plugins
[dplug
->h
.id
].file_ops
;
177 inode
->i_mapping
->a_ops
= &dir_plugins
[dplug
->h
.id
].as_ops
;
180 assert("vs-46", fplug
!= NULL
);
181 assert("vs-43", (fplug
->h
.id
== UNIX_FILE_PLUGIN_ID
||
182 fplug
->h
.id
== CRYPTCOMPRESS_FILE_PLUGIN_ID
));
183 inode
->i_op
= &file_plugins
[fplug
->h
.id
].inode_ops
;
184 inode
->i_fop
= &file_plugins
[fplug
->h
.id
].file_ops
;
185 inode
->i_mapping
->a_ops
= &file_plugins
[fplug
->h
.id
].as_ops
;
188 warning("nikita-291", "wrong file mode: %o for %llu",
190 (unsigned long long)get_inode_oid(inode
));
191 reiser4_make_bad_inode(inode
);
192 return RETERR(-EINVAL
);
197 /* Initialize inode from disk data. Called with inode locked.
198 Return inode locked. */
199 static int init_inode(struct inode
*inode
/* inode to intialise */ ,
200 coord_t
* coord
/* coord of stat data */ )
206 reiser4_inode
*state
;
208 assert("nikita-292", coord
!= NULL
);
209 assert("nikita-293", inode
!= NULL
);
211 coord_clear_iplug(coord
);
212 result
= zload(coord
->node
);
215 iplug
= item_plugin_by_coord(coord
);
216 body
= item_body_by_coord(coord
);
217 length
= item_length_by_coord(coord
);
219 assert("nikita-295", iplug
!= NULL
);
220 assert("nikita-296", body
!= NULL
);
221 assert("nikita-297", length
> 0);
223 /* inode is under I_LOCK now */
225 state
= reiser4_inode_data(inode
);
226 /* call stat-data plugin method to load sd content into inode */
227 result
= iplug
->s
.sd
.init_inode(inode
, body
, length
);
228 set_plugin(&state
->pset
, PSET_SD
, item_plugin_to_plugin(iplug
));
230 result
= setup_inode_ops(inode
, NULL
);
231 if (result
== 0 && inode
->i_sb
->s_root
&&
232 inode
->i_sb
->s_root
->d_inode
)
233 result
= finish_pset(inode
);
239 /* read `inode' from the disk. This is what was previously in
240 reiserfs_read_inode2().
242 Must be called with inode locked. Return inode still locked.
244 static int read_inode(struct inode
*inode
/* inode to read from disk */ ,
245 const reiser4_key
* key
/* key of stat data */ ,
253 assert("nikita-298", inode
!= NULL
);
254 assert("nikita-1945", !is_inode_loaded(inode
));
256 info
= reiser4_inode_data(inode
);
257 assert("nikita-300", info
->locality_id
!= 0);
259 coord_init_zero(&coord
);
261 /* locate stat-data in a tree and return znode locked */
262 result
= lookup_sd(inode
, ZNODE_READ_LOCK
, &coord
, &lh
, key
, silent
);
263 assert("nikita-301", !is_inode_loaded(inode
));
265 /* use stat-data plugin to load sd into inode. */
266 result
= init_inode(inode
, &coord
);
268 /* initialize stat-data seal */
269 spin_lock_inode(inode
);
270 reiser4_seal_init(&info
->sd_seal
, &coord
, key
);
271 info
->sd_coord
= coord
;
272 spin_unlock_inode(inode
);
274 /* call file plugin's method to initialize plugin
275 * specific part of inode */
276 if (inode_file_plugin(inode
)->init_inode_data
)
277 inode_file_plugin(inode
)->init_inode_data(inode
,
280 /* load detached directory cursors for stateless
281 * directory readers (NFS). */
282 reiser4_load_cursors(inode
);
284 /* Check the opened inode for consistency. */
286 get_super_private(inode
->i_sb
)->df_plug
->
290 /* lookup_sd() doesn't release coord because we want znode
291 stay read-locked while stat-data fields are accessed in
296 reiser4_make_bad_inode(inode
);
300 /* initialise new reiser4 inode being inserted into hash table. */
301 static int init_locked_inode(struct inode
*inode
/* new inode */ ,
302 void *opaque
/* key of stat data passed to the
303 * iget5_locked as cookie */ )
307 assert("nikita-1995", inode
!= NULL
);
308 assert("nikita-1996", opaque
!= NULL
);
310 set_inode_oid(inode
, get_key_objectid(key
));
311 reiser4_inode_data(inode
)->locality_id
= get_key_locality(key
);
315 /* reiser4_inode_find_actor() - "find actor" supplied by reiser4 to iget5_locked().
317 This function is called by iget5_locked() to distinguish reiser4 inodes
318 having the same inode numbers. Such inodes can only exist due to some error
319 condition. One of them should be bad. Inodes with identical inode numbers
320 (objectids) are distinguished by their packing locality.
323 static int reiser4_inode_find_actor(struct inode
*inode
/* inode from hash table to
325 void *opaque
/* "cookie" passed to
326 * iget5_locked(). This is stat data
333 /* oid is unique, so first term is enough, actually. */
334 get_inode_oid(inode
) == get_key_objectid(key
) &&
336 * also, locality should be checked, but locality is stored in
337 * the reiser4-specific part of the inode, and actor can be
338 * called against arbitrary inode that happened to be in this
339 * hash chain. Hence we first have to check that this is
340 * reiser4 inode at least. is_reiser4_inode() is probably too
341 * early to call, as inode may have ->i_op not yet
344 is_reiser4_super(inode
->i_sb
) &&
346 * usually objectid is unique, but pseudo files use counter to
347 * generate objectid. All pseudo files are placed into special
348 * (otherwise unused) locality.
350 reiser4_inode_data(inode
)->locality_id
== get_key_locality(key
);
353 /* hook for kmem_cache_create */
354 void loading_init_once(reiser4_inode
* info
)
356 mutex_init(&info
->loading
);
359 /* for reiser4_alloc_inode */
360 void loading_alloc(reiser4_inode
* info
)
362 assert("vs-1717", !mutex_is_locked(&info
->loading
));
365 /* for reiser4_destroy */
366 void loading_destroy(reiser4_inode
* info
)
368 assert("vs-1717a", !mutex_is_locked(&info
->loading
));
371 static void loading_begin(reiser4_inode
* info
)
373 mutex_lock(&info
->loading
);
376 static void loading_end(reiser4_inode
* info
)
378 mutex_unlock(&info
->loading
);
382 * reiser4_iget - obtain inode via iget5_locked, read from disk if necessary
383 * @super: super block of filesystem
384 * @key: key of inode's stat-data
387 * This is our helper function a la iget(). This is be called by
388 * lookup_common() and reiser4_read_super(). Return inode locked or error
391 struct inode
*reiser4_iget(struct super_block
*super
, const reiser4_key
*key
,
398 assert("nikita-302", super
!= NULL
);
399 assert("nikita-303", key
!= NULL
);
403 /* call iget(). Our ->read_inode() is dummy, so this will either
404 find inode in cache or return uninitialised inode */
405 inode
= iget5_locked(super
,
406 (unsigned long)get_key_objectid(key
),
407 reiser4_inode_find_actor
,
408 init_locked_inode
, (reiser4_key
*) key
);
410 return ERR_PTR(RETERR(-ENOMEM
));
411 if (is_bad_inode(inode
)) {
412 warning("nikita-304", "Bad inode found");
413 reiser4_print_key("key", key
);
415 return ERR_PTR(RETERR(-EIO
));
418 info
= reiser4_inode_data(inode
);
420 /* Reiser4 inode state bit REISER4_LOADED is used to distinguish fully
421 loaded and initialized inode from just allocated inode. If
422 REISER4_LOADED bit is not set, reiser4_iget() completes loading under
423 info->loading. The place in reiser4 which uses not initialized inode
424 is the reiser4 repacker, see repacker-related functions in
425 plugin/item/extent.c */
426 if (!is_inode_loaded(inode
)) {
428 if (!is_inode_loaded(inode
)) {
429 /* locking: iget5_locked returns locked inode */
430 assert("nikita-1941", !is_inode_loaded(inode
));
431 assert("nikita-1949",
432 reiser4_inode_find_actor(inode
,
433 (reiser4_key
*) key
));
434 /* now, inode has objectid as ->i_ino and locality in
435 reiser4-specific part. This is enough for
436 read_inode() to read stat data from the disk */
437 result
= read_inode(inode
, key
, silent
);
442 if (inode
->i_state
& I_NEW
)
443 unlock_new_inode(inode
);
445 if (is_bad_inode(inode
)) {
446 assert("vs-1717", result
!= 0);
449 inode
= ERR_PTR(result
);
450 } else if (REISER4_DEBUG
) {
451 reiser4_key found_key
;
453 assert("vs-1717", result
== 0);
454 build_sd_key(inode
, &found_key
);
455 if (!keyeq(&found_key
, key
)) {
456 warning("nikita-305", "Wrong key in sd");
457 reiser4_print_key("sought for", key
);
458 reiser4_print_key("found", &found_key
);
460 if (inode
->i_nlink
== 0) {
461 warning("nikita-3559", "Unlinked inode found: %llu\n",
462 (unsigned long long)get_inode_oid(inode
));
468 /* reiser4_iget() may return not fully initialized inode, this function should
469 * be called after one completes reiser4 inode initializing. */
470 void reiser4_iget_complete(struct inode
*inode
)
472 assert("zam-988", is_reiser4_inode(inode
));
474 if (!is_inode_loaded(inode
)) {
475 reiser4_inode_set_flag(inode
, REISER4_LOADED
);
476 loading_end(reiser4_inode_data(inode
));
480 void reiser4_make_bad_inode(struct inode
*inode
)
482 assert("nikita-1934", inode
!= NULL
);
484 /* clear LOADED bit */
485 reiser4_inode_clr_flag(inode
, REISER4_LOADED
);
486 make_bad_inode(inode
);
490 file_plugin
*inode_file_plugin(const struct inode
* inode
)
492 assert("nikita-1997", inode
!= NULL
);
493 return reiser4_inode_data(inode
)->pset
->file
;
496 dir_plugin
*inode_dir_plugin(const struct inode
* inode
)
498 assert("nikita-1998", inode
!= NULL
);
499 return reiser4_inode_data(inode
)->pset
->dir
;
502 formatting_plugin
*inode_formatting_plugin(const struct inode
* inode
)
504 assert("nikita-2000", inode
!= NULL
);
505 return reiser4_inode_data(inode
)->pset
->formatting
;
508 hash_plugin
*inode_hash_plugin(const struct inode
* inode
)
510 assert("nikita-2001", inode
!= NULL
);
511 return reiser4_inode_data(inode
)->pset
->hash
;
514 fibration_plugin
*inode_fibration_plugin(const struct inode
* inode
)
516 assert("nikita-2001", inode
!= NULL
);
517 return reiser4_inode_data(inode
)->pset
->fibration
;
520 cipher_plugin
*inode_cipher_plugin(const struct inode
* inode
)
522 assert("edward-36", inode
!= NULL
);
523 return reiser4_inode_data(inode
)->pset
->cipher
;
526 compression_plugin
*inode_compression_plugin(const struct inode
* inode
)
528 assert("edward-37", inode
!= NULL
);
529 return reiser4_inode_data(inode
)->pset
->compression
;
532 compression_mode_plugin
*inode_compression_mode_plugin(const struct inode
*
535 assert("edward-1330", inode
!= NULL
);
536 return reiser4_inode_data(inode
)->pset
->compression_mode
;
539 cluster_plugin
*inode_cluster_plugin(const struct inode
* inode
)
541 assert("edward-1328", inode
!= NULL
);
542 return reiser4_inode_data(inode
)->pset
->cluster
;
545 file_plugin
*inode_create_plugin(const struct inode
* inode
)
547 assert("edward-1329", inode
!= NULL
);
548 return reiser4_inode_data(inode
)->pset
->create
;
551 digest_plugin
*inode_digest_plugin(const struct inode
* inode
)
553 assert("edward-86", inode
!= NULL
);
554 return reiser4_inode_data(inode
)->pset
->digest
;
557 item_plugin
*inode_sd_plugin(const struct inode
* inode
)
559 assert("vs-534", inode
!= NULL
);
560 return reiser4_inode_data(inode
)->pset
->sd
;
563 item_plugin
*inode_dir_item_plugin(const struct inode
* inode
)
565 assert("vs-534", inode
!= NULL
);
566 return reiser4_inode_data(inode
)->pset
->dir_item
;
569 file_plugin
*child_create_plugin(const struct inode
* inode
)
571 assert("edward-1329", inode
!= NULL
);
572 return reiser4_inode_data(inode
)->hset
->create
;
575 void inode_set_extension(struct inode
*inode
, sd_ext_bits ext
)
577 reiser4_inode
*state
;
579 assert("nikita-2716", inode
!= NULL
);
580 assert("nikita-2717", ext
< LAST_SD_EXTENSION
);
581 assert("nikita-3491", spin_inode_is_locked(inode
));
583 state
= reiser4_inode_data(inode
);
584 state
->extmask
|= 1 << ext
;
585 /* force re-calculation of stat-data length on next call to
587 reiser4_inode_clr_flag(inode
, REISER4_SDLEN_KNOWN
);
590 void inode_clr_extension(struct inode
*inode
, sd_ext_bits ext
)
592 reiser4_inode
*state
;
594 assert("vpf-1926", inode
!= NULL
);
595 assert("vpf-1927", ext
< LAST_SD_EXTENSION
);
596 assert("vpf-1928", spin_inode_is_locked(inode
));
598 state
= reiser4_inode_data(inode
);
599 state
->extmask
&= ~(1 << ext
);
600 /* force re-calculation of stat-data length on next call to
602 reiser4_inode_clr_flag(inode
, REISER4_SDLEN_KNOWN
);
605 void inode_check_scale_nolock(struct inode
*inode
, __u64 old
, __u64
new)
607 assert("edward-1287", inode
!= NULL
);
608 if (!dscale_fit(old
, new))
609 reiser4_inode_clr_flag(inode
, REISER4_SDLEN_KNOWN
);
613 void inode_check_scale(struct inode
*inode
, __u64 old
, __u64
new)
615 assert("nikita-2875", inode
!= NULL
);
616 spin_lock_inode(inode
);
617 inode_check_scale_nolock(inode
, old
, new);
618 spin_unlock_inode(inode
);
622 * initialize ->ordering field of inode. This field defines how file stat-data
623 * and body is ordered within a tree with respect to other objects within the
624 * same parent directory.
627 init_inode_ordering(struct inode
*inode
,
628 reiser4_object_create_data
* crd
, int create
)
633 struct inode
*parent
;
635 parent
= crd
->parent
;
636 assert("nikita-3224", inode_dir_plugin(parent
) != NULL
);
637 inode_dir_plugin(parent
)->build_entry_key(parent
,
638 &crd
->dentry
->d_name
,
643 coord
= &reiser4_inode_data(inode
)->sd_coord
;
644 coord_clear_iplug(coord
);
645 /* safe to use ->sd_coord, because node is under long term
647 WITH_DATA(coord
->node
, item_key_by_coord(coord
, &key
));
650 set_inode_ordering(inode
, get_key_ordering(&key
));
653 znode
*inode_get_vroot(struct inode
*inode
)
655 reiser4_block_nr blk
;
658 spin_lock_inode(inode
);
659 blk
= reiser4_inode_data(inode
)->vroot
;
660 spin_unlock_inode(inode
);
661 if (!disk_addr_eq(&UBER_TREE_ADDR
, &blk
))
662 result
= zlook(reiser4_tree_by_inode(inode
), &blk
);
668 void inode_set_vroot(struct inode
*inode
, znode
*vroot
)
670 spin_lock_inode(inode
);
671 reiser4_inode_data(inode
)->vroot
= *znode_get_block(vroot
);
672 spin_unlock_inode(inode
);
677 void reiser4_inode_invariant(const struct inode
*inode
)
679 assert("nikita-3077", spin_inode_is_locked(inode
));
682 int inode_has_no_jnodes(reiser4_inode
* r4_inode
)
684 return jnode_tree_by_reiser4_inode(r4_inode
)->rnode
== NULL
&&
685 r4_inode
->nr_jnodes
== 0;
690 /* true if directory is empty (only contains dot and dotdot) */
691 /* FIXME: shouldn't it be dir plugin method? */
692 int is_dir_empty(const struct inode
*dir
)
694 assert("nikita-1976", dir
!= NULL
);
696 /* rely on our method to maintain directory i_size being equal to the
697 number of entries. */
698 return dir
->i_size
<= 2 ? 0 : RETERR(-ENOTEMPTY
);
703 c-indentation-style: "K&R"