revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / reiser4 / plugin / inode_ops.c
blob48430f70264b5be18fb1c12ac7ec542abf7773c5
1 /*
2 * Copyright 2005 by Hans Reiser, licensing governed by reiser4/README
3 */
5 /*
6 * this file contains typical implementations for most of methods of struct
7 * inode_operations
8 */
10 #include "../inode.h"
11 #include "../safe_link.h"
13 #include <linux/quotaops.h>
14 #include <linux/namei.h>
16 static int create_vfs_object(struct inode *parent, struct dentry *dentry,
17 reiser4_object_create_data *data);
19 /**
20 * reiser4_create_common - create of inode operations
21 * @parent: inode of parent directory
22 * @dentry: dentry of new object to create
23 * @mode: the permissions to use
24 * @nameidata:
26 * This is common implementation of vfs's create method of struct
27 * inode_operations.
28 * Creates regular file using file plugin from parent directory plugin set.
30 int reiser4_create_common(struct inode *parent, struct dentry *dentry,
31 int mode, struct nameidata *nameidata)
33 reiser4_object_create_data data;
34 file_plugin *fplug;
36 memset(&data, 0, sizeof data);
37 data.mode = S_IFREG | mode;
38 fplug = child_create_plugin(parent) ? : inode_create_plugin(parent);
39 if (!plugin_of_group(fplug, REISER4_REGULAR_FILE)) {
40 warning("vpf-1900", "'%s' is not a regular file plugin.",
41 fplug->h.label);
42 return RETERR(-EIO);
44 data.id = fplug->h.id;
45 return create_vfs_object(parent, dentry, &data);
48 int reiser4_lookup_name(struct inode *dir, struct dentry *, reiser4_key *);
49 void check_light_weight(struct inode *inode, struct inode *parent);
51 /**
52 * reiser4_lookup_common - lookup of inode operations
53 * @parent: inode of directory to lookup into
54 * @dentry: name to look for
55 * @nameidata:
57 * This is common implementation of vfs's lookup method of struct
58 * inode_operations.
60 struct dentry *reiser4_lookup_common(struct inode *parent,
61 struct dentry *dentry,
62 struct nameidata *nameidata)
64 reiser4_context *ctx;
65 int result;
66 struct dentry *new;
67 struct inode *inode;
68 reiser4_dir_entry_desc entry;
70 ctx = reiser4_init_context(parent->i_sb);
71 if (IS_ERR(ctx))
72 return (struct dentry *)ctx;
74 /* set up operations on dentry. */
75 dentry->d_op = &get_super_private(parent->i_sb)->ops.dentry;
77 result = reiser4_lookup_name(parent, dentry, &entry.key);
78 if (result) {
79 context_set_commit_async(ctx);
80 reiser4_exit_context(ctx);
81 if (result == -ENOENT) {
82 /* object not found */
83 if (!IS_DEADDIR(parent))
84 d_add(dentry, NULL);
85 return NULL;
87 return ERR_PTR(result);
90 inode = reiser4_iget(parent->i_sb, &entry.key, 0);
91 if (IS_ERR(inode)) {
92 context_set_commit_async(ctx);
93 reiser4_exit_context(ctx);
94 return ERR_PTR(PTR_ERR(inode));
97 /* success */
98 check_light_weight(inode, parent);
99 new = d_splice_alias(inode, dentry);
100 reiser4_iget_complete(inode);
102 /* prevent balance_dirty_pages() from being called: we don't want to
103 * do this under directory i_mutex. */
104 context_set_commit_async(ctx);
105 reiser4_exit_context(ctx);
106 return new;
109 static reiser4_block_nr common_estimate_link(struct inode *parent,
110 struct inode *object);
111 int reiser4_update_dir(struct inode *);
114 * reiser4_link_common - link of inode operations
115 * @existing: dentry of object which is to get new name
116 * @parent: directory where new name is to be created
117 * @newname: new name
119 * This is common implementation of vfs's link method of struct
120 * inode_operations.
122 int reiser4_link_common(struct dentry *existing, struct inode *parent,
123 struct dentry *newname)
125 reiser4_context *ctx;
126 int result;
127 struct inode *object;
128 dir_plugin *parent_dplug;
129 reiser4_dir_entry_desc entry;
130 reiser4_object_create_data data;
131 reiser4_block_nr reserve;
133 ctx = reiser4_init_context(parent->i_sb);
134 if (IS_ERR(ctx))
135 return PTR_ERR(ctx);
137 assert("nikita-1431", existing != NULL);
138 assert("nikita-1432", parent != NULL);
139 assert("nikita-1433", newname != NULL);
141 object = existing->d_inode;
142 assert("nikita-1434", object != NULL);
144 /* check for race with create_object() */
145 if (reiser4_inode_get_flag(object, REISER4_IMMUTABLE)) {
146 context_set_commit_async(ctx);
147 reiser4_exit_context(ctx);
148 return RETERR(-E_REPEAT);
151 parent_dplug = inode_dir_plugin(parent);
153 memset(&entry, 0, sizeof entry);
154 entry.obj = object;
156 data.mode = object->i_mode;
157 data.id = inode_file_plugin(object)->h.id;
159 reserve = common_estimate_link(parent, existing->d_inode);
160 if ((__s64) reserve < 0) {
161 context_set_commit_async(ctx);
162 reiser4_exit_context(ctx);
163 return reserve;
166 if (reiser4_grab_space(reserve, BA_CAN_COMMIT)) {
167 context_set_commit_async(ctx);
168 reiser4_exit_context(ctx);
169 return RETERR(-ENOSPC);
173 * Subtle race handling: sys_link() doesn't take i_mutex on @parent. It
174 * means that link(2) can race against unlink(2) or rename(2), and
175 * inode is dead (->i_nlink == 0) when reiser4_link() is entered.
177 * For such inode we have to undo special processing done in
178 * reiser4_unlink() viz. creation of safe-link.
180 if (unlikely(object->i_nlink == 0)) {
181 result = safe_link_del(reiser4_tree_by_inode(object),
182 get_inode_oid(object), SAFE_UNLINK);
183 if (result != 0) {
184 context_set_commit_async(ctx);
185 reiser4_exit_context(ctx);
186 return result;
190 /* increment nlink of @existing and update its stat data */
191 result = reiser4_add_nlink(object, parent, 1);
192 if (result == 0) {
193 /* add entry to the parent */
194 result =
195 parent_dplug->add_entry(parent, newname, &data, &entry);
196 if (result != 0) {
197 /* failed to add entry to the parent, decrement nlink
198 of @existing */
199 reiser4_del_nlink(object, parent, 1);
201 * now, if that failed, we have a file with too big
202 * nlink---space leak, much better than directory
203 * entry pointing to nowhere
207 if (result == 0) {
208 atomic_inc(&object->i_count);
210 * Upon successful completion, link() shall mark for update
211 * the st_ctime field of the file. Also, the st_ctime and
212 * st_mtime fields of the directory that contains the new
213 * entry shall be marked for update. --SUS
215 result = reiser4_update_dir(parent);
217 if (result == 0)
218 d_instantiate(newname, existing->d_inode);
220 context_set_commit_async(ctx);
221 reiser4_exit_context(ctx);
222 return result;
225 static int unlink_check_and_grab(struct inode *parent, struct dentry *victim);
228 * reiser4_unlink_common - unlink of inode operations
229 * @parent: inode of directory to remove name from
230 * @victim: name to be removed
232 * This is common implementation of vfs's unlink method of struct
233 * inode_operations.
235 int reiser4_unlink_common(struct inode *parent, struct dentry *victim)
237 reiser4_context *ctx;
238 int result;
239 struct inode *object;
240 file_plugin *fplug;
242 ctx = reiser4_init_context(parent->i_sb);
243 if (IS_ERR(ctx))
244 return PTR_ERR(ctx);
246 object = victim->d_inode;
247 fplug = inode_file_plugin(object);
248 assert("nikita-2882", fplug->detach != NULL);
250 result = unlink_check_and_grab(parent, victim);
251 if (result != 0) {
252 context_set_commit_async(ctx);
253 reiser4_exit_context(ctx);
254 return result;
257 result = fplug->detach(object, parent);
258 if (result == 0) {
259 dir_plugin *parent_dplug;
260 reiser4_dir_entry_desc entry;
262 parent_dplug = inode_dir_plugin(parent);
263 memset(&entry, 0, sizeof entry);
265 /* first, delete directory entry */
266 result = parent_dplug->rem_entry(parent, victim, &entry);
267 if (result == 0) {
269 * if name was removed successfully, we _have_ to
270 * return 0 from this function, because upper level
271 * caller (vfs_{rmdir,unlink}) expect this.
273 * now that directory entry is removed, update
274 * stat-data
276 reiser4_del_nlink(object, parent, 1);
278 * Upon successful completion, unlink() shall mark for
279 * update the st_ctime and st_mtime fields of the
280 * parent directory. Also, if the file's link count is
281 * not 0, the st_ctime field of the file shall be
282 * marked for update. --SUS
284 reiser4_update_dir(parent);
285 /* add safe-link for this file */
286 if (object->i_nlink == 0)
287 safe_link_add(object, SAFE_UNLINK);
291 if (unlikely(result != 0)) {
292 if (result != -ENOMEM)
293 warning("nikita-3398", "Cannot unlink %llu (%i)",
294 (unsigned long long)get_inode_oid(object),
295 result);
296 /* if operation failed commit pending inode modifications to
297 * the stat-data */
298 reiser4_update_sd(object);
299 reiser4_update_sd(parent);
302 reiser4_release_reserved(object->i_sb);
304 /* @object's i_ctime was updated by ->rem_link() method(). */
306 /* @victim can be already removed from the disk by this time. Inode is
307 then marked so that iput() wouldn't try to remove stat data. But
308 inode itself is still there.
312 * we cannot release directory semaphore here, because name has
313 * already been deleted, but dentry (@victim) still exists. Prevent
314 * balance_dirty_pages() from being called on exiting this context: we
315 * don't want to do this under directory i_mutex.
317 context_set_commit_async(ctx);
318 reiser4_exit_context(ctx);
319 return result;
323 * reiser4_symlink_common - symlink of inode operations
324 * @parent: inode of parent directory
325 * @dentry: dentry of object to be created
326 * @linkname: string symlink is to contain
328 * This is common implementation of vfs's symlink method of struct
329 * inode_operations.
330 * Creates object using file plugin SYMLINK_FILE_PLUGIN_ID.
332 int reiser4_symlink_common(struct inode *parent, struct dentry *dentry,
333 const char *linkname)
335 reiser4_object_create_data data;
337 memset(&data, 0, sizeof data);
338 data.name = linkname;
339 data.id = SYMLINK_FILE_PLUGIN_ID;
340 data.mode = S_IFLNK | S_IRWXUGO;
341 return create_vfs_object(parent, dentry, &data);
345 * reiser4_mkdir_common - mkdir of inode operations
346 * @parent: inode of parent directory
347 * @dentry: dentry of object to be created
348 * @mode: the permissions to use
350 * This is common implementation of vfs's mkdir method of struct
351 * inode_operations.
352 * Creates object using file plugin DIRECTORY_FILE_PLUGIN_ID.
354 int reiser4_mkdir_common(struct inode *parent, struct dentry *dentry, int mode)
356 reiser4_object_create_data data;
358 memset(&data, 0, sizeof data);
359 data.mode = S_IFDIR | mode;
360 data.id = DIRECTORY_FILE_PLUGIN_ID;
361 return create_vfs_object(parent, dentry, &data);
365 * reiser4_mknod_common - mknod of inode operations
366 * @parent: inode of parent directory
367 * @dentry: dentry of object to be created
368 * @mode: the permissions to use and file type
369 * @rdev: minor and major of new device file
371 * This is common implementation of vfs's mknod method of struct
372 * inode_operations.
373 * Creates object using file plugin SPECIAL_FILE_PLUGIN_ID.
375 int reiser4_mknod_common(struct inode *parent, struct dentry *dentry,
376 int mode, dev_t rdev)
378 reiser4_object_create_data data;
380 memset(&data, 0, sizeof data);
381 data.mode = mode;
382 data.rdev = rdev;
383 data.id = SPECIAL_FILE_PLUGIN_ID;
384 return create_vfs_object(parent, dentry, &data);
388 * implementation of vfs's rename method of struct inode_operations for typical
389 * directory is in inode_ops_rename.c
393 * reiser4_follow_link_common - follow_link of inode operations
394 * @dentry: dentry of symlink
395 * @data:
397 * This is common implementation of vfs's followlink method of struct
398 * inode_operations.
399 * Assumes that inode's i_private points to the content of symbolic link.
401 void *reiser4_follow_link_common(struct dentry *dentry, struct nameidata *nd)
403 assert("vs-851", S_ISLNK(dentry->d_inode->i_mode));
405 if (!dentry->d_inode->i_private
406 || !reiser4_inode_get_flag(dentry->d_inode,
407 REISER4_GENERIC_PTR_USED))
408 return ERR_PTR(RETERR(-EINVAL));
409 nd_set_link(nd, dentry->d_inode->i_private);
410 return NULL;
414 * reiser4_permission_common - permission of inode operations
415 * @inode: inode to check permissions for
416 * @mask: mode bits to check permissions for
417 * @nameidata:
419 * Uses generic function to check for rwx permissions.
421 int reiser4_permission_common(struct inode *inode, int mask,
422 struct nameidata *nameidata)
424 return generic_permission(inode, mask, NULL);
427 static int setattr_reserve(reiser4_tree *);
429 /* this is common implementation of vfs's setattr method of struct
430 inode_operations
432 int reiser4_setattr_common(struct dentry *dentry, struct iattr *attr)
434 reiser4_context *ctx;
435 struct inode *inode;
436 int result;
438 inode = dentry->d_inode;
439 result = inode_change_ok(inode, attr);
440 if (result)
441 return result;
443 ctx = reiser4_init_context(inode->i_sb);
444 if (IS_ERR(ctx))
445 return PTR_ERR(ctx);
447 assert("nikita-3119", !(attr->ia_valid & ATTR_SIZE));
450 * grab disk space and call standard inode_setattr().
452 result = setattr_reserve(reiser4_tree_by_inode(inode));
453 if (!result) {
454 if ((attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid)
455 || (attr->ia_valid & ATTR_GID
456 && attr->ia_gid != inode->i_gid)) {
457 result = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
458 if (result) {
459 context_set_commit_async(ctx);
460 reiser4_exit_context(ctx);
461 return result;
464 result = inode_setattr(inode, attr);
465 if (!result)
466 reiser4_update_sd(inode);
469 context_set_commit_async(ctx);
470 reiser4_exit_context(ctx);
471 return result;
474 /* this is common implementation of vfs's getattr method of struct
475 inode_operations
477 int reiser4_getattr_common(struct vfsmount *mnt UNUSED_ARG,
478 struct dentry *dentry, struct kstat *stat)
480 struct inode *obj;
482 assert("nikita-2298", dentry != NULL);
483 assert("nikita-2299", stat != NULL);
484 assert("nikita-2300", dentry->d_inode != NULL);
486 obj = dentry->d_inode;
488 stat->dev = obj->i_sb->s_dev;
489 stat->ino = oid_to_uino(get_inode_oid(obj));
490 stat->mode = obj->i_mode;
491 /* don't confuse userland with huge nlink. This is not entirely
492 * correct, because nlink_t is not necessary 16 bit signed. */
493 stat->nlink = min(obj->i_nlink, (typeof(obj->i_nlink)) 0x7fff);
494 stat->uid = obj->i_uid;
495 stat->gid = obj->i_gid;
496 stat->rdev = obj->i_rdev;
497 stat->atime = obj->i_atime;
498 stat->mtime = obj->i_mtime;
499 stat->ctime = obj->i_ctime;
500 stat->size = obj->i_size;
501 stat->blocks =
502 (inode_get_bytes(obj) + VFS_BLKSIZE - 1) >> VFS_BLKSIZE_BITS;
503 /* "preferred" blocksize for efficient file system I/O */
504 stat->blksize = get_super_private(obj->i_sb)->optimal_io_size;
506 return 0;
509 /* Estimate the maximum amount of nodes which might be allocated or changed on
510 typical new object creation. Typical creation consists of calling create
511 method of file plugin, adding directory entry to parent and update parent
512 directory's stat data.
514 static reiser4_block_nr estimate_create_vfs_object(struct inode *parent, /* parent object */
515 struct inode *object
516 /* object */ )
518 assert("vpf-309", parent != NULL);
519 assert("vpf-307", object != NULL);
521 return
522 /* object creation estimation */
523 inode_file_plugin(object)->estimate.create(object) +
524 /* stat data of parent directory estimation */
525 inode_file_plugin(parent)->estimate.update(parent) +
526 /* adding entry estimation */
527 inode_dir_plugin(parent)->estimate.add_entry(parent) +
528 /* to undo in the case of failure */
529 inode_dir_plugin(parent)->estimate.rem_entry(parent);
532 /* Create child in directory.
534 . get object's plugin
535 . get fresh inode
536 . initialize inode
537 . add object's stat-data
538 . initialize object's directory
539 . add entry to the parent
540 . instantiate dentry
543 static int do_create_vfs_child(reiser4_object_create_data * data, /* parameters of new
544 object */
545 struct inode **retobj)
547 int result;
549 struct dentry *dentry; /* parent object */
550 struct inode *parent; /* new name */
552 dir_plugin *par_dir; /* directory plugin on the parent */
553 dir_plugin *obj_dir; /* directory plugin on the new object */
554 file_plugin *obj_plug; /* object plugin on the new object */
555 struct inode *object; /* new object */
556 reiser4_block_nr reserve;
558 reiser4_dir_entry_desc entry; /* new directory entry */
560 assert("nikita-1420", data != NULL);
561 parent = data->parent;
562 dentry = data->dentry;
564 assert("nikita-1418", parent != NULL);
565 assert("nikita-1419", dentry != NULL);
567 /* check, that name is acceptable for parent */
568 par_dir = inode_dir_plugin(parent);
569 if (par_dir->is_name_acceptable &&
570 !par_dir->is_name_acceptable(parent,
571 dentry->d_name.name,
572 (int)dentry->d_name.len))
573 return RETERR(-ENAMETOOLONG);
575 result = 0;
576 obj_plug = file_plugin_by_id((int)data->id);
577 if (obj_plug == NULL) {
578 warning("nikita-430", "Cannot find plugin %i", data->id);
579 return RETERR(-ENOENT);
581 object = new_inode(parent->i_sb);
582 if (object == NULL)
583 return RETERR(-ENOMEM);
584 /* we'll update i_nlink below */
585 object->i_nlink = 0;
586 /* new_inode() initializes i_ino to "arbitrary" value. Reset it to 0,
587 * to simplify error handling: if some error occurs before i_ino is
588 * initialized with oid, i_ino should already be set to some
589 * distinguished value. */
590 object->i_ino = 0;
592 /* So that on error iput will be called. */
593 *retobj = object;
595 if (DQUOT_ALLOC_INODE(object)) {
596 DQUOT_DROP(object);
597 object->i_flags |= S_NOQUOTA;
598 return RETERR(-EDQUOT);
601 memset(&entry, 0, sizeof entry);
602 entry.obj = object;
604 set_plugin(&reiser4_inode_data(object)->pset, PSET_FILE,
605 file_plugin_to_plugin(obj_plug));
606 result = obj_plug->set_plug_in_inode(object, parent, data);
607 if (result) {
608 warning("nikita-431", "Cannot install plugin %i on %llx",
609 data->id, (unsigned long long)get_inode_oid(object));
610 DQUOT_FREE_INODE(object);
611 object->i_flags |= S_NOQUOTA;
612 return result;
615 /* reget plugin after installation */
616 obj_plug = inode_file_plugin(object);
618 if (obj_plug->create_object == NULL) {
619 DQUOT_FREE_INODE(object);
620 object->i_flags |= S_NOQUOTA;
621 return RETERR(-EPERM);
624 /* if any of hash, tail, sd or permission plugins for newly created
625 object are not set yet set them here inheriting them from parent
626 directory
628 assert("nikita-2070", obj_plug->adjust_to_parent != NULL);
629 result = obj_plug->adjust_to_parent(object,
630 parent,
631 object->i_sb->s_root->d_inode);
632 if (result == 0)
633 result = finish_pset(object);
634 if (result != 0) {
635 warning("nikita-432", "Cannot inherit from %llx to %llx",
636 (unsigned long long)get_inode_oid(parent),
637 (unsigned long long)get_inode_oid(object));
638 DQUOT_FREE_INODE(object);
639 object->i_flags |= S_NOQUOTA;
640 return result;
643 /* setup inode and file-operations for this inode */
644 setup_inode_ops(object, data);
646 /* call file plugin's method to initialize plugin specific part of
647 * inode */
648 if (obj_plug->init_inode_data)
649 obj_plug->init_inode_data(object, data, 1 /*create */ );
651 /* obtain directory plugin (if any) for new object. */
652 obj_dir = inode_dir_plugin(object);
653 if (obj_dir != NULL && obj_dir->init == NULL) {
654 DQUOT_FREE_INODE(object);
655 object->i_flags |= S_NOQUOTA;
656 return RETERR(-EPERM);
659 reiser4_inode_data(object)->locality_id = get_inode_oid(parent);
661 reserve = estimate_create_vfs_object(parent, object);
662 if (reiser4_grab_space(reserve, BA_CAN_COMMIT)) {
663 DQUOT_FREE_INODE(object);
664 object->i_flags |= S_NOQUOTA;
665 return RETERR(-ENOSPC);
668 /* mark inode `immutable'. We disable changes to the file being
669 created until valid directory entry for it is inserted. Otherwise,
670 if file were expanded and insertion of directory entry fails, we
671 have to remove file, but we only alloted enough space in
672 transaction to remove _empty_ file. 3.x code used to remove stat
673 data in different transaction thus possibly leaking disk space on
674 crash. This all only matters if it's possible to access file
675 without name, for example, by inode number
677 reiser4_inode_set_flag(object, REISER4_IMMUTABLE);
679 /* create empty object, this includes allocation of new objectid. For
680 directories this implies creation of dot and dotdot */
681 assert("nikita-2265", reiser4_inode_get_flag(object, REISER4_NO_SD));
683 /* mark inode as `loaded'. From this point onward
684 reiser4_delete_inode() will try to remove its stat-data. */
685 reiser4_inode_set_flag(object, REISER4_LOADED);
687 result = obj_plug->create_object(object, parent, data);
688 if (result != 0) {
689 reiser4_inode_clr_flag(object, REISER4_IMMUTABLE);
690 if (result != -ENAMETOOLONG && result != -ENOMEM)
691 warning("nikita-2219",
692 "Failed to create sd for %llu",
693 (unsigned long long)get_inode_oid(object));
694 DQUOT_FREE_INODE(object);
695 object->i_flags |= S_NOQUOTA;
696 return result;
699 if (obj_dir != NULL)
700 result = obj_dir->init(object, parent, data);
701 if (result == 0) {
702 assert("nikita-434", !reiser4_inode_get_flag(object,
703 REISER4_NO_SD));
704 /* insert inode into VFS hash table */
705 insert_inode_hash(object);
706 /* create entry */
707 result = par_dir->add_entry(parent, dentry, data, &entry);
708 if (result == 0) {
709 result = reiser4_add_nlink(object, parent, 0);
710 /* If O_CREAT is set and the file did not previously
711 exist, upon successful completion, open() shall
712 mark for update the st_atime, st_ctime, and
713 st_mtime fields of the file and the st_ctime and
714 st_mtime fields of the parent directory. --SUS
716 /* @object times are already updated by
717 reiser4_add_nlink() */
718 if (result == 0)
719 reiser4_update_dir(parent);
720 if (result != 0)
721 /* cleanup failure to add nlink */
722 par_dir->rem_entry(parent, dentry, &entry);
724 if (result != 0)
725 /* cleanup failure to add entry */
726 obj_plug->detach(object, parent);
727 } else if (result != -ENOMEM)
728 warning("nikita-2219", "Failed to initialize dir for %llu: %i",
729 (unsigned long long)get_inode_oid(object), result);
732 * update stat-data, committing all pending modifications to the inode
733 * fields.
735 reiser4_update_sd(object);
736 if (result != 0) {
737 DQUOT_FREE_INODE(object);
738 object->i_flags |= S_NOQUOTA;
739 /* if everything was ok (result == 0), parent stat-data is
740 * already updated above (update_parent_dir()) */
741 reiser4_update_sd(parent);
742 /* failure to create entry, remove object */
743 obj_plug->delete_object(object);
746 /* file has name now, clear immutable flag */
747 reiser4_inode_clr_flag(object, REISER4_IMMUTABLE);
749 /* on error, iput() will call ->delete_inode(). We should keep track
750 of the existence of stat-data for this inode and avoid attempt to
751 remove it in reiser4_delete_inode(). This is accomplished through
752 REISER4_NO_SD bit in inode.u.reiser4_i.plugin.flags
754 return result;
757 /* this is helper for common implementations of reiser4_mkdir, reiser4_create,
758 reiser4_mknod and reiser4_symlink
760 static int
761 create_vfs_object(struct inode *parent,
762 struct dentry *dentry, reiser4_object_create_data * data)
764 reiser4_context *ctx;
765 int result;
766 struct inode *child;
768 ctx = reiser4_init_context(parent->i_sb);
769 if (IS_ERR(ctx))
770 return PTR_ERR(ctx);
771 context_set_commit_async(ctx);
773 data->parent = parent;
774 data->dentry = dentry;
775 child = NULL;
776 result = do_create_vfs_child(data, &child);
777 if (unlikely(result != 0)) {
778 if (child != NULL) {
779 reiser4_make_bad_inode(child);
780 iput(child);
782 } else
783 d_instantiate(dentry, child);
785 reiser4_exit_context(ctx);
786 return result;
789 /* helper for link_common. Estimate disk space necessary to add a link
790 from @parent to @object
792 static reiser4_block_nr common_estimate_link(struct inode *parent, /* parent directory */
793 struct inode *object
794 /* object to which new link is being cerated */
797 reiser4_block_nr res = 0;
798 file_plugin *fplug;
799 dir_plugin *dplug;
801 assert("vpf-317", object != NULL);
802 assert("vpf-318", parent != NULL);
804 fplug = inode_file_plugin(object);
805 dplug = inode_dir_plugin(parent);
806 /* VS-FIXME-HANS: why do we do fplug->estimate.update(object) twice instead of multiplying by 2? */
807 /* reiser4_add_nlink(object) */
808 res += fplug->estimate.update(object);
809 /* add_entry(parent) */
810 res += dplug->estimate.add_entry(parent);
811 /* reiser4_del_nlink(object) */
812 res += fplug->estimate.update(object);
813 /* update_dir(parent) */
814 res += inode_file_plugin(parent)->estimate.update(parent);
815 /* safe-link */
816 res += estimate_one_item_removal(reiser4_tree_by_inode(object));
818 return res;
821 /* Estimate disk space necessary to remove a link between @parent and
822 @object.
824 static reiser4_block_nr estimate_unlink(struct inode *parent, /* parent directory */
825 struct inode *object
826 /* object to which new link is being cerated */
829 reiser4_block_nr res = 0;
830 file_plugin *fplug;
831 dir_plugin *dplug;
833 assert("vpf-317", object != NULL);
834 assert("vpf-318", parent != NULL);
836 fplug = inode_file_plugin(object);
837 dplug = inode_dir_plugin(parent);
839 /* rem_entry(parent) */
840 res += dplug->estimate.rem_entry(parent);
841 /* reiser4_del_nlink(object) */
842 res += fplug->estimate.update(object);
843 /* update_dir(parent) */
844 res += inode_file_plugin(parent)->estimate.update(parent);
845 /* fplug->unlink */
846 res += fplug->estimate.unlink(object, parent);
847 /* safe-link */
848 res += estimate_one_insert_item(reiser4_tree_by_inode(object));
850 return res;
853 /* helper for reiser4_unlink_common. Estimate and grab space for unlink. */
854 static int unlink_check_and_grab(struct inode *parent, struct dentry *victim)
856 file_plugin *fplug;
857 struct inode *child;
858 int result;
860 result = 0;
861 child = victim->d_inode;
862 fplug = inode_file_plugin(child);
864 /* check for race with create_object() */
865 if (reiser4_inode_get_flag(child, REISER4_IMMUTABLE))
866 return RETERR(-E_REPEAT);
867 /* object being deleted should have stat data */
868 assert("vs-949", !reiser4_inode_get_flag(child, REISER4_NO_SD));
870 /* ask object plugin */
871 if (fplug->can_rem_link != NULL && !fplug->can_rem_link(child))
872 return RETERR(-ENOTEMPTY);
874 result = (int)estimate_unlink(parent, child);
875 if (result < 0)
876 return result;
878 return reiser4_grab_reserved(child->i_sb, result, BA_CAN_COMMIT);
881 /* helper for reiser4_setattr_common */
882 static int setattr_reserve(reiser4_tree * tree)
884 assert("vs-1096", is_grab_enabled(get_current_context()));
885 return reiser4_grab_space(estimate_one_insert_into_item(tree),
886 BA_CAN_COMMIT);
889 /* helper function. Standards require that for many file-system operations
890 on success ctime and mtime of parent directory is to be updated. */
891 int reiser4_update_dir(struct inode *dir)
893 assert("nikita-2525", dir != NULL);
895 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
896 return reiser4_update_sd(dir);