mm-only debug patch...
[mmotm.git] / fs / reiser4 / inode.c
blobcc4a401da2ba046311cff6a16f6229f87a427f0c
1 /* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by
2 reiser4/README */
4 /* Inode specific operations. */
6 #include "forward.h"
7 #include "debug.h"
8 #include "key.h"
9 #include "kassign.h"
10 #include "coord.h"
11 #include "seal.h"
12 #include "dscale.h"
13 #include "plugin/item/item.h"
14 #include "plugin/security/perm.h"
15 #include "plugin/plugin.h"
16 #include "plugin/object.h"
17 #include "znode.h"
18 #include "vfs_ops.h"
19 #include "inode.h"
20 #include "super.h"
21 #include "reiser4.h"
23 #include <linux/fs.h> /* for struct super_block, address_space */
25 /* return reiser4 internal tree which inode belongs to */
26 /* Audited by: green(2002.06.17) */
27 reiser4_tree *reiser4_tree_by_inode(const struct inode *inode/* inode queried*/)
29 assert("nikita-256", inode != NULL);
30 assert("nikita-257", inode->i_sb != NULL);
31 return reiser4_get_tree(inode->i_sb);
34 /* return reiser4-specific inode flags */
35 static inline unsigned long *inode_flags(const struct inode *const inode)
37 assert("nikita-2842", inode != NULL);
38 return &reiser4_inode_data(inode)->flags;
41 /* set reiser4-specific flag @f in @inode */
42 void reiser4_inode_set_flag(struct inode *inode, reiser4_file_plugin_flags f)
44 assert("nikita-2248", inode != NULL);
45 set_bit((int)f, inode_flags(inode));
48 /* clear reiser4-specific flag @f in @inode */
49 void reiser4_inode_clr_flag(struct inode *inode, reiser4_file_plugin_flags f)
51 assert("nikita-2250", inode != NULL);
52 clear_bit((int)f, inode_flags(inode));
55 /* true if reiser4-specific flag @f is set in @inode */
56 int reiser4_inode_get_flag(const struct inode *inode,
57 reiser4_file_plugin_flags f)
59 assert("nikita-2251", inode != NULL);
60 return test_bit((int)f, inode_flags(inode));
63 /* convert oid to inode number */
64 ino_t oid_to_ino(oid_t oid)
66 return (ino_t) oid;
69 /* convert oid to user visible inode number */
70 ino_t oid_to_uino(oid_t oid)
72 /* reiser4 object is uniquely identified by oid which is 64 bit
73 quantity. Kernel in-memory inode is indexed (in the hash table) by
74 32 bit i_ino field, but this is not a problem, because there is a
75 way to further distinguish inodes with identical inode numbers
76 (find_actor supplied to iget()).
78 But user space expects unique 32 bit inode number. Obviously this
79 is impossible. Work-around is to somehow hash oid into user visible
80 inode number.
82 oid_t max_ino = (ino_t) ~0;
84 if (REISER4_INO_IS_OID || (oid <= max_ino))
85 return oid;
86 else
87 /* this is remotely similar to algorithm used to find next pid
88 to use for process: after wrap-around start from some
89 offset rather than from 0. Idea is that there are some long
90 living objects with which we don't want to collide.
92 return REISER4_UINO_SHIFT + ((oid - max_ino) & (max_ino >> 1));
95 /* check that "inode" is on reiser4 file-system */
96 int is_reiser4_inode(const struct inode *inode/* inode queried */)
98 return inode != NULL && is_reiser4_super(inode->i_sb);
101 /* Maximal length of a name that can be stored in directory @inode.
103 This is used in check during file creation and lookup. */
104 int reiser4_max_filename_len(const struct inode *inode/* inode queried */)
106 assert("nikita-287", is_reiser4_inode(inode));
107 assert("nikita-1710", inode_dir_item_plugin(inode));
108 if (inode_dir_item_plugin(inode)->s.dir.max_name_len)
109 return inode_dir_item_plugin(inode)->s.dir.max_name_len(inode);
110 else
111 return 255;
114 #if REISER4_USE_COLLISION_LIMIT
115 /* Maximal number of hash collisions for this directory. */
116 int max_hash_collisions(const struct inode *dir/* inode queried */)
118 assert("nikita-1711", dir != NULL);
119 return reiser4_inode_data(dir)->plugin.max_collisions;
121 #endif /* REISER4_USE_COLLISION_LIMIT */
123 /* Install file, inode, and address_space operation on @inode, depending on
124 its mode. */
125 int setup_inode_ops(struct inode *inode /* inode to intialize */ ,
126 reiser4_object_create_data * data /* parameters to create
127 * object */ )
129 reiser4_super_info_data *sinfo;
130 file_plugin *fplug;
131 dir_plugin *dplug;
133 fplug = inode_file_plugin(inode);
134 dplug = inode_dir_plugin(inode);
136 sinfo = get_super_private(inode->i_sb);
138 switch (inode->i_mode & S_IFMT) {
139 case S_IFSOCK:
140 case S_IFBLK:
141 case S_IFCHR:
142 case S_IFIFO:
144 dev_t rdev; /* to keep gcc happy */
146 assert("vs-46", fplug != NULL);
147 /* ugly hack with rdev */
148 if (data == NULL) {
149 rdev = inode->i_rdev;
150 inode->i_rdev = 0;
151 } else
152 rdev = data->rdev;
153 inode->i_blocks = 0;
154 assert("vs-42", fplug->h.id == SPECIAL_FILE_PLUGIN_ID);
155 inode->i_op = file_plugins[fplug->h.id].inode_ops;
156 /* initialize inode->i_fop and inode->i_rdev for block
157 and char devices */
158 init_special_inode(inode, inode->i_mode, rdev);
159 /* all address space operations are null */
160 inode->i_mapping->a_ops =
161 file_plugins[fplug->h.id].as_ops;
162 break;
164 case S_IFLNK:
165 assert("vs-46", fplug != NULL);
166 assert("vs-42", fplug->h.id == SYMLINK_FILE_PLUGIN_ID);
167 inode->i_op = file_plugins[fplug->h.id].inode_ops;
168 inode->i_fop = NULL;
169 /* all address space operations are null */
170 inode->i_mapping->a_ops = file_plugins[fplug->h.id].as_ops;
171 break;
172 case S_IFDIR:
173 assert("vs-46", dplug != NULL);
174 assert("vs-43", (dplug->h.id == HASHED_DIR_PLUGIN_ID ||
175 dplug->h.id == SEEKABLE_HASHED_DIR_PLUGIN_ID));
176 inode->i_op = dir_plugins[dplug->h.id].inode_ops;
177 inode->i_fop = dir_plugins[dplug->h.id].file_ops;
178 inode->i_mapping->a_ops = dir_plugins[dplug->h.id].as_ops;
179 break;
180 case S_IFREG:
181 assert("vs-46", fplug != NULL);
182 assert("vs-43", (fplug->h.id == UNIX_FILE_PLUGIN_ID ||
183 fplug->h.id == CRYPTCOMPRESS_FILE_PLUGIN_ID));
184 inode->i_op = file_plugins[fplug->h.id].inode_ops;
185 inode->i_fop = file_plugins[fplug->h.id].file_ops;
186 inode->i_mapping->a_ops = file_plugins[fplug->h.id].as_ops;
187 break;
188 default:
189 warning("nikita-291", "wrong file mode: %o for %llu",
190 inode->i_mode,
191 (unsigned long long)get_inode_oid(inode));
192 reiser4_make_bad_inode(inode);
193 return RETERR(-EINVAL);
195 return 0;
198 /* Initialize inode from disk data. Called with inode locked.
199 Return inode locked. */
200 static int init_inode(struct inode *inode /* inode to intialise */ ,
201 coord_t *coord/* coord of stat data */)
203 int result;
204 item_plugin *iplug;
205 void *body;
206 int length;
207 reiser4_inode *state;
209 assert("nikita-292", coord != NULL);
210 assert("nikita-293", inode != NULL);
212 coord_clear_iplug(coord);
213 result = zload(coord->node);
214 if (result)
215 return result;
216 iplug = item_plugin_by_coord(coord);
217 body = item_body_by_coord(coord);
218 length = item_length_by_coord(coord);
220 assert("nikita-295", iplug != NULL);
221 assert("nikita-296", body != NULL);
222 assert("nikita-297", length > 0);
224 /* inode is under I_LOCK now */
226 state = reiser4_inode_data(inode);
227 /* call stat-data plugin method to load sd content into inode */
228 result = iplug->s.sd.init_inode(inode, body, length);
229 set_plugin(&state->pset, PSET_SD, item_plugin_to_plugin(iplug));
230 if (result == 0) {
231 result = setup_inode_ops(inode, NULL);
232 if (result == 0 && inode->i_sb->s_root &&
233 inode->i_sb->s_root->d_inode)
234 result = finish_pset(inode);
236 zrelse(coord->node);
237 return result;
240 /* read `inode' from the disk. This is what was previously in
241 reiserfs_read_inode2().
243 Must be called with inode locked. Return inode still locked.
245 static int read_inode(struct inode *inode /* inode to read from disk */ ,
246 const reiser4_key * key /* key of stat data */ ,
247 int silent)
249 int result;
250 lock_handle lh;
251 reiser4_inode *info;
252 coord_t coord;
254 assert("nikita-298", inode != NULL);
255 assert("nikita-1945", !is_inode_loaded(inode));
257 info = reiser4_inode_data(inode);
258 assert("nikita-300", info->locality_id != 0);
260 coord_init_zero(&coord);
261 init_lh(&lh);
262 /* locate stat-data in a tree and return znode locked */
263 result = lookup_sd(inode, ZNODE_READ_LOCK, &coord, &lh, key, silent);
264 assert("nikita-301", !is_inode_loaded(inode));
265 if (result == 0) {
266 /* use stat-data plugin to load sd into inode. */
267 result = init_inode(inode, &coord);
268 if (result == 0) {
269 /* initialize stat-data seal */
270 spin_lock_inode(inode);
271 reiser4_seal_init(&info->sd_seal, &coord, key);
272 info->sd_coord = coord;
273 spin_unlock_inode(inode);
275 /* call file plugin's method to initialize plugin
276 * specific part of inode */
277 if (inode_file_plugin(inode)->init_inode_data)
278 inode_file_plugin(inode)->init_inode_data(inode,
279 NULL,
281 /* load detached directory cursors for stateless
282 * directory readers (NFS). */
283 reiser4_load_cursors(inode);
285 /* Check the opened inode for consistency. */
286 result =
287 get_super_private(inode->i_sb)->df_plug->
288 check_open(inode);
291 /* lookup_sd() doesn't release coord because we want znode
292 stay read-locked while stat-data fields are accessed in
293 init_inode() */
294 done_lh(&lh);
296 if (result != 0)
297 reiser4_make_bad_inode(inode);
298 return result;
301 /* initialise new reiser4 inode being inserted into hash table. */
302 static int init_locked_inode(struct inode *inode /* new inode */ ,
303 void *opaque /* key of stat data passed to
304 * the iget5_locked as cookie */)
306 reiser4_key *key;
308 assert("nikita-1995", inode != NULL);
309 assert("nikita-1996", opaque != NULL);
310 key = opaque;
311 set_inode_oid(inode, get_key_objectid(key));
312 reiser4_inode_data(inode)->locality_id = get_key_locality(key);
313 return 0;
316 /* reiser4_inode_find_actor() - "find actor" supplied by reiser4 to
317 iget5_locked().
319 This function is called by iget5_locked() to distinguish reiser4 inodes
320 having the same inode numbers. Such inodes can only exist due to some error
321 condition. One of them should be bad. Inodes with identical inode numbers
322 (objectids) are distinguished by their packing locality.
325 static int reiser4_inode_find_actor(struct inode *inode /* inode from hash table
326 * to check */ ,
327 void *opaque /* "cookie" passed to
328 * iget5_locked(). This
329 * is stat-data key */)
331 reiser4_key *key;
333 key = opaque;
334 return
335 /* oid is unique, so first term is enough, actually. */
336 get_inode_oid(inode) == get_key_objectid(key) &&
338 * also, locality should be checked, but locality is stored in
339 * the reiser4-specific part of the inode, and actor can be
340 * called against arbitrary inode that happened to be in this
341 * hash chain. Hence we first have to check that this is
342 * reiser4 inode at least. is_reiser4_inode() is probably too
343 * early to call, as inode may have ->i_op not yet
344 * initialised.
346 is_reiser4_super(inode->i_sb) &&
348 * usually objectid is unique, but pseudo files use counter to
349 * generate objectid. All pseudo files are placed into special
350 * (otherwise unused) locality.
352 reiser4_inode_data(inode)->locality_id == get_key_locality(key);
355 /* hook for kmem_cache_create */
356 void loading_init_once(reiser4_inode * info)
358 mutex_init(&info->loading);
361 /* for reiser4_alloc_inode */
362 void loading_alloc(reiser4_inode * info)
364 assert("vs-1717", !mutex_is_locked(&info->loading));
367 /* for reiser4_destroy */
368 void loading_destroy(reiser4_inode * info)
370 assert("vs-1717a", !mutex_is_locked(&info->loading));
373 static void loading_begin(reiser4_inode * info)
375 mutex_lock(&info->loading);
378 static void loading_end(reiser4_inode * info)
380 mutex_unlock(&info->loading);
384 * reiser4_iget - obtain inode via iget5_locked, read from disk if necessary
385 * @super: super block of filesystem
386 * @key: key of inode's stat-data
387 * @silent:
389 * This is our helper function a la iget(). This is be called by
390 * lookup_common() and reiser4_read_super(). Return inode locked or error
391 * encountered.
393 struct inode *reiser4_iget(struct super_block *super, const reiser4_key *key,
394 int silent)
396 struct inode *inode;
397 int result;
398 reiser4_inode *info;
400 assert("nikita-302", super != NULL);
401 assert("nikita-303", key != NULL);
403 result = 0;
405 /* call iget(). Our ->read_inode() is dummy, so this will either
406 find inode in cache or return uninitialised inode */
407 inode = iget5_locked(super,
408 (unsigned long)get_key_objectid(key),
409 reiser4_inode_find_actor,
410 init_locked_inode, (reiser4_key *) key);
411 if (inode == NULL)
412 return ERR_PTR(RETERR(-ENOMEM));
413 if (is_bad_inode(inode)) {
414 warning("nikita-304", "Bad inode found");
415 reiser4_print_key("key", key);
416 iput(inode);
417 return ERR_PTR(RETERR(-EIO));
420 info = reiser4_inode_data(inode);
422 /* Reiser4 inode state bit REISER4_LOADED is used to distinguish fully
423 loaded and initialized inode from just allocated inode. If
424 REISER4_LOADED bit is not set, reiser4_iget() completes loading under
425 info->loading. The place in reiser4 which uses not initialized inode
426 is the reiser4 repacker, see repacker-related functions in
427 plugin/item/extent.c */
428 if (!is_inode_loaded(inode)) {
429 loading_begin(info);
430 if (!is_inode_loaded(inode)) {
431 /* locking: iget5_locked returns locked inode */
432 assert("nikita-1941", !is_inode_loaded(inode));
433 assert("nikita-1949",
434 reiser4_inode_find_actor(inode,
435 (reiser4_key *) key));
436 /* now, inode has objectid as ->i_ino and locality in
437 reiser4-specific part. This is enough for
438 read_inode() to read stat data from the disk */
439 result = read_inode(inode, key, silent);
440 } else
441 loading_end(info);
444 if (inode->i_state & I_NEW)
445 unlock_new_inode(inode);
447 if (is_bad_inode(inode)) {
448 assert("vs-1717", result != 0);
449 loading_end(info);
450 iput(inode);
451 inode = ERR_PTR(result);
452 } else if (REISER4_DEBUG) {
453 reiser4_key found_key;
455 assert("vs-1717", result == 0);
456 build_sd_key(inode, &found_key);
457 if (!keyeq(&found_key, key)) {
458 warning("nikita-305", "Wrong key in sd");
459 reiser4_print_key("sought for", key);
460 reiser4_print_key("found", &found_key);
462 if (inode->i_nlink == 0) {
463 warning("nikita-3559", "Unlinked inode found: %llu\n",
464 (unsigned long long)get_inode_oid(inode));
467 return inode;
470 /* reiser4_iget() may return not fully initialized inode, this function should
471 * be called after one completes reiser4 inode initializing. */
472 void reiser4_iget_complete(struct inode *inode)
474 assert("zam-988", is_reiser4_inode(inode));
476 if (!is_inode_loaded(inode)) {
477 reiser4_inode_set_flag(inode, REISER4_LOADED);
478 loading_end(reiser4_inode_data(inode));
482 void reiser4_make_bad_inode(struct inode *inode)
484 assert("nikita-1934", inode != NULL);
486 /* clear LOADED bit */
487 reiser4_inode_clr_flag(inode, REISER4_LOADED);
488 make_bad_inode(inode);
489 return;
492 file_plugin *inode_file_plugin(const struct inode *inode)
494 assert("nikita-1997", inode != NULL);
495 return reiser4_inode_data(inode)->pset->file;
498 dir_plugin *inode_dir_plugin(const struct inode *inode)
500 assert("nikita-1998", inode != NULL);
501 return reiser4_inode_data(inode)->pset->dir;
504 formatting_plugin *inode_formatting_plugin(const struct inode *inode)
506 assert("nikita-2000", inode != NULL);
507 return reiser4_inode_data(inode)->pset->formatting;
510 hash_plugin *inode_hash_plugin(const struct inode *inode)
512 assert("nikita-2001", inode != NULL);
513 return reiser4_inode_data(inode)->pset->hash;
516 fibration_plugin *inode_fibration_plugin(const struct inode *inode)
518 assert("nikita-2001", inode != NULL);
519 return reiser4_inode_data(inode)->pset->fibration;
522 cipher_plugin *inode_cipher_plugin(const struct inode *inode)
524 assert("edward-36", inode != NULL);
525 return reiser4_inode_data(inode)->pset->cipher;
528 compression_plugin *inode_compression_plugin(const struct inode *inode)
530 assert("edward-37", inode != NULL);
531 return reiser4_inode_data(inode)->pset->compression;
534 compression_mode_plugin *inode_compression_mode_plugin(const struct inode *
535 inode)
537 assert("edward-1330", inode != NULL);
538 return reiser4_inode_data(inode)->pset->compression_mode;
541 cluster_plugin *inode_cluster_plugin(const struct inode *inode)
543 assert("edward-1328", inode != NULL);
544 return reiser4_inode_data(inode)->pset->cluster;
547 file_plugin *inode_create_plugin(const struct inode *inode)
549 assert("edward-1329", inode != NULL);
550 return reiser4_inode_data(inode)->pset->create;
553 digest_plugin *inode_digest_plugin(const struct inode *inode)
555 assert("edward-86", inode != NULL);
556 return reiser4_inode_data(inode)->pset->digest;
559 item_plugin *inode_sd_plugin(const struct inode *inode)
561 assert("vs-534", inode != NULL);
562 return reiser4_inode_data(inode)->pset->sd;
565 item_plugin *inode_dir_item_plugin(const struct inode *inode)
567 assert("vs-534", inode != NULL);
568 return reiser4_inode_data(inode)->pset->dir_item;
571 file_plugin *child_create_plugin(const struct inode *inode)
573 assert("edward-1329", inode != NULL);
574 return reiser4_inode_data(inode)->hset->create;
577 void inode_set_extension(struct inode *inode, sd_ext_bits ext)
579 reiser4_inode *state;
581 assert("nikita-2716", inode != NULL);
582 assert("nikita-2717", ext < LAST_SD_EXTENSION);
583 assert("nikita-3491", spin_inode_is_locked(inode));
585 state = reiser4_inode_data(inode);
586 state->extmask |= 1 << ext;
587 /* force re-calculation of stat-data length on next call to
588 update_sd(). */
589 reiser4_inode_clr_flag(inode, REISER4_SDLEN_KNOWN);
592 void inode_clr_extension(struct inode *inode, sd_ext_bits ext)
594 reiser4_inode *state;
596 assert("vpf-1926", inode != NULL);
597 assert("vpf-1927", ext < LAST_SD_EXTENSION);
598 assert("vpf-1928", spin_inode_is_locked(inode));
600 state = reiser4_inode_data(inode);
601 state->extmask &= ~(1 << ext);
602 /* force re-calculation of stat-data length on next call to
603 update_sd(). */
604 reiser4_inode_clr_flag(inode, REISER4_SDLEN_KNOWN);
607 void inode_check_scale_nolock(struct inode *inode, __u64 old, __u64 new)
609 assert("edward-1287", inode != NULL);
610 if (!dscale_fit(old, new))
611 reiser4_inode_clr_flag(inode, REISER4_SDLEN_KNOWN);
612 return;
615 void inode_check_scale(struct inode *inode, __u64 old, __u64 new)
617 assert("nikita-2875", inode != NULL);
618 spin_lock_inode(inode);
619 inode_check_scale_nolock(inode, old, new);
620 spin_unlock_inode(inode);
624 * initialize ->ordering field of inode. This field defines how file stat-data
625 * and body is ordered within a tree with respect to other objects within the
626 * same parent directory.
628 void
629 init_inode_ordering(struct inode *inode,
630 reiser4_object_create_data * crd, int create)
632 reiser4_key key;
634 if (create) {
635 struct inode *parent;
637 parent = crd->parent;
638 assert("nikita-3224", inode_dir_plugin(parent) != NULL);
639 inode_dir_plugin(parent)->build_entry_key(parent,
640 &crd->dentry->d_name,
641 &key);
642 } else {
643 coord_t *coord;
645 coord = &reiser4_inode_data(inode)->sd_coord;
646 coord_clear_iplug(coord);
647 /* safe to use ->sd_coord, because node is under long term
648 * lock */
649 WITH_DATA(coord->node, item_key_by_coord(coord, &key));
652 set_inode_ordering(inode, get_key_ordering(&key));
655 znode *inode_get_vroot(struct inode *inode)
657 reiser4_block_nr blk;
658 znode *result;
660 spin_lock_inode(inode);
661 blk = reiser4_inode_data(inode)->vroot;
662 spin_unlock_inode(inode);
663 if (!disk_addr_eq(&UBER_TREE_ADDR, &blk))
664 result = zlook(reiser4_tree_by_inode(inode), &blk);
665 else
666 result = NULL;
667 return result;
670 void inode_set_vroot(struct inode *inode, znode *vroot)
672 spin_lock_inode(inode);
673 reiser4_inode_data(inode)->vroot = *znode_get_block(vroot);
674 spin_unlock_inode(inode);
677 #if REISER4_DEBUG
679 void reiser4_inode_invariant(const struct inode *inode)
681 assert("nikita-3077", spin_inode_is_locked(inode));
684 int inode_has_no_jnodes(reiser4_inode * r4_inode)
686 return jnode_tree_by_reiser4_inode(r4_inode)->rnode == NULL &&
687 r4_inode->nr_jnodes == 0;
690 #endif
692 /* true if directory is empty (only contains dot and dotdot) */
693 /* FIXME: shouldn't it be dir plugin method? */
694 int is_dir_empty(const struct inode *dir)
696 assert("nikita-1976", dir != NULL);
698 /* rely on our method to maintain directory i_size being equal to the
699 number of entries. */
700 return dir->i_size <= 2 ? 0 : RETERR(-ENOTEMPTY);
703 /* Make Linus happy.
704 Local variables:
705 c-indentation-style: "K&R"
706 mode-name: "LC"
707 c-basic-offset: 8
708 tab-width: 8
709 fill-column: 120
710 End: