1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * derived in small part from linux/fs/ext2/namei.c
23 * Copyright (C) 1991, 1992 Linus Torvalds
25 * Big-endian to little-endian byte-swapping/bitmaps by
26 * David S. Miller (davem@caip.rutgers.edu), 1995
27 * Directory entry file type support and forward compatibility hooks
28 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
32 #include <linux/sched.h>
34 #include <linux/smp_lock.h>
35 #include <linux/quotaops.h>
36 #include <linux/highmem.h>
37 #include <linux/pagemap.h>
39 #define DEBUG_SUBSYSTEM S_LLITE
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lite.h>
43 #include <linux/lustre_dlm.h>
46 extern void ll_set_dd(struct dentry *de);
49 extern void ll_change_inode(struct inode *inode);
50 extern int ll_setattr(struct dentry *de, struct iattr *attr);
53 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
54 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
55 int ext2_make_empty(struct inode *inode, struct inode *parent);
56 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
57 struct dentry *dentry, struct page ** res_page);
58 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
59 int ext2_empty_dir (struct inode * inode);
60 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
61 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
62 struct page *page, struct inode *inode);
65 * Couple of helper functions - make the code slightly cleaner.
67 static inline void ext2_inc_count(struct inode *inode)
72 /* postpone the disk update until the inode really goes away */
73 static inline void ext2_dec_count(struct inode *inode)
77 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
80 err = ll_add_link(dentry, inode);
82 d_instantiate(dentry, inode);
85 ext2_dec_count(inode);
92 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
93 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
95 static int ll_test_inode(struct inode *inode, void *opaque)
98 struct ll_read_inode2_cookie *lic = opaque;
99 struct mds_body *body = lic->lic_body;
101 if (inode->i_generation != lic->lic_body->generation)
104 /* Apply the attributes in 'opaque' to this inode */
105 ll_update_inode(inode, body, lic->lic_lmm);
110 extern struct dentry_operations ll_d_ops;
112 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
116 ldlm_lock_decref(lockh, mode);
121 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
122 extern int ll_read_inode2(struct inode *inode, void *opaque);
123 struct inode *ll_iget(struct super_block *sb, ino_t hash,
124 struct ll_read_inode2_cookie *lic)
129 inode = iget5_locked(sb, hash, ll_test_inode, ll_read_inode2, lic);
132 return ERR_PTR(-ENOMEM);
134 if (inode->i_state & I_NEW)
135 unlock_new_inode(inode);
137 // XXX Coda always fills inodes, should Lustre?
141 struct inode *ll_iget(struct super_block *sb, ino_t hash,
142 struct ll_read_inode2_cookie *lic)
146 inode = iget4(sb, hash, ll_find_inode, lic);
151 static int ll_intent_to_lock_mode(struct lookup_intent *it)
153 /* CREAT needs to be tested before open (both could be set) */
154 if (it->it_op & (IT_CREAT | IT_SETATTR))
156 else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP))
163 int ll_it_open_error(int phase, struct lookup_intent *it)
165 if (it->it_disposition & IT_OPEN_OPEN) {
166 if (phase == IT_OPEN_OPEN)
167 return it->it_status;
172 if (it->it_disposition & IT_OPEN_CREATE) {
173 if (phase == IT_OPEN_CREATE)
174 return it->it_status;
179 if (it->it_disposition & IT_OPEN_LOOKUP) {
180 if (phase == IT_OPEN_LOOKUP)
181 return it->it_status;
189 #define IT_ENQ_COMPLETE (1<<16)
191 int ll_intent_lock(struct inode *parent, struct dentry **de,
192 struct lookup_intent *it, intent_finish_cb intent_finish)
194 struct dentry *dentry = *de;
195 struct ll_sb_info *sbi = ll_i2sbi(parent);
196 struct lustre_handle lockh;
197 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
198 struct ptlrpc_request *request = NULL;
200 int rc = 0, datalen = 0, offset, flag = 0;
207 CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
208 dentry->d_name.name, ldlm_it2str(it->it_op));
210 if (dentry->d_name.len > EXT2_NAME_LEN)
211 RETURN(-ENAMETOOLONG);
213 if (!(it->it_disposition & IT_ENQ_COMPLETE)) {
214 rc = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_PLAIN, it,
215 ll_intent_to_lock_mode(it), parent, dentry,
216 &lockh, data, datalen, parent,
220 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
223 request = (struct ptlrpc_request *)it->it_data;
225 /* non-zero it_disposition indicates that the server performed the
226 * intent on our behalf. */
227 if (it->it_disposition) {
228 struct mds_body *mds_body;
231 /* This long block is all about fixing up the local
232 * state so that it is correct as of the moment
233 * _before_ the operation was applied; that way, the
234 * VFS will think that everything is normal and call
235 * Lustre's regular FS function.
237 * If we're performing a creation, that means that unless the
238 * creation failed with EEXIST, we should fake up a negative
239 * dentry. Likewise for the target of a hard link.
241 * For everything else, we want to lookup to succeed. */
243 /* One additional note: if CREATE/MKDIR/etc succeeded,
244 * we add an extra reference to the request because we
245 * need to keep it around until ll_create gets called.
246 * For anything else which results in
247 * LL_LOOKUP_POSITIVE, we can do the iget()
248 * immediately with the contents of the reply (in the
249 * intent_finish callback). In the create case,
250 * however, we need to wait until ll_create_node to do
251 * the iget() or the VFS will abort with -EEXISTS.
255 mds_body = lustre_msg_buf(request->rq_repmsg, offset);
256 ino = mds_body->fid1.id;
257 mode = mds_body->mode;
259 /*We were called from revalidate2: did we find the same inode?*/
260 if ((*de)->d_inode &&
261 (ino != (*de)->d_inode->i_ino ||
262 mds_body->fid1.generation != (*de)->d_inode->i_generation)) {
263 it->it_disposition |= IT_ENQ_COMPLETE;
267 /* If we're doing an IT_OPEN which did not result in an actual
268 * successful open, then we need to remove the bit which saves
269 * this request for unconditional replay. */
270 if (it->it_op & IT_OPEN &&
271 (!(it->it_disposition & IT_OPEN_OPEN) ||
273 request->rq_flags &= ~PTL_RPC_FL_REPLAY;
275 if (it->it_op & IT_CREAT) {
276 mdc_store_inode_generation(request, 2, 1);
277 /* The server will return to us, in it_disposition, an
278 * indication of exactly what it_status refers to.
280 * If IT_OPEN_OPEN is set, then it_status refers to the
281 * open() call, otherwise if IT_OPEN_CREATE is set, then
282 * it status is the creation failure mode. In either
283 * case, one of IT_OPEN_NEG or IT_OPEN_POS will be set,
284 * indicating whether the child lookup was successful.
286 * Else, if IT_OPEN_LOOKUP then it_status is the rc
287 * of the child lookup.
289 * Finally, if none of the bits are set, then the
290 * failure occurred while looking up the parent. */
291 rc = ll_it_open_error(IT_OPEN_LOOKUP, it);
295 if (it->it_disposition & IT_OPEN_CREATE)
296 ptlrpc_request_addref(request);
298 if (it->it_disposition & IT_OPEN_NEG)
299 flag = LL_LOOKUP_NEGATIVE;
301 flag = LL_LOOKUP_POSITIVE;
302 } else if (it->it_op == IT_OPEN) {
303 LASSERT(!(it->it_disposition & IT_OPEN_CREATE));
305 rc = ll_it_open_error(IT_OPEN_LOOKUP, it);
309 if (it->it_disposition & IT_OPEN_OPEN)
310 ptlrpc_request_addref(request);
312 if (it->it_disposition & IT_OPEN_NEG)
313 flag = LL_LOOKUP_NEGATIVE;
315 flag = LL_LOOKUP_POSITIVE;
316 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP)) {
317 /* For check ops, we want the lookup to succeed */
320 flag = LL_LOOKUP_NEGATIVE;
322 flag = LL_LOOKUP_POSITIVE;
329 LBUG(); /* For the moment, no non-intent locks */
331 /* it_disposition == 0 indicates that it just did a simple lock
332 * request, for which we are very thankful. move along with
333 * the local lookup then. */
335 //memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
338 ino = ll_inode_by_name(parent, dentry, &mode);
340 CERROR("inode %*s not found by name\n",
341 dentry->d_name.len, dentry->d_name.name);
342 GOTO(drop_lock, rc = -ENOENT);
345 valid = OBD_MD_FLNOTOBD;
348 datalen = obd_size_wiremd(&sbi->ll_osc_conn, NULL),
349 valid |= OBD_MD_FLEASIZE;
351 valid |= OBD_MD_FLBLOCKS;
354 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode, valid,
357 CERROR("failure %d inode "LPX64"\n", rc, ino);
358 GOTO(drop_req, rc = -abs(rc));
362 if (intent_finish != NULL) {
363 rc = intent_finish(flag, request, de, it, offset, ino);
364 dentry = *de; /* intent_finish may change *de */
366 ptlrpc_req_finished(request);
369 /* This places the intent in the dentry so that the vfs_xxx
370 * operation can lay its hands on it; but that is not always
371 * needed... (we need to save it in the GETATTR case for the
372 * benefit of ll_inode_revalidate -phil) */
373 if (it->it_op & (IT_OPEN | IT_GETATTR))
374 LL_SAVE_INTENT(dentry, it);
377 "D_IT dentry %p fsdata %p intent: %s status %d\n",
378 dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
381 if (it->it_op == IT_LOOKUP)
382 ll_intent_release(dentry, it);
387 ptlrpc_req_finished(request);
389 #warning FIXME: must release lock here
393 /* Search "inode"'s alias list for a dentry that has the same name and parent as
394 * de. If found, return it. If not found, return de. */
395 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
397 struct list_head *tmp;
399 spin_lock(&dcache_lock);
400 list_for_each(tmp, &inode->i_dentry) {
401 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
403 /* We are called here with 'de' already on the aliases list. */
409 if (dentry->d_parent != de->d_parent)
412 if (dentry->d_name.len != de->d_name.len)
415 if (memcmp(dentry->d_name.name, de->d_name.name,
416 de->d_name.len) != 0)
419 if (!list_empty(&dentry->d_lru))
420 list_del_init(&dentry->d_lru);
422 list_del_init(&dentry->d_hash);
423 spin_unlock(&dcache_lock);
425 atomic_inc(&dentry->d_count);
427 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
431 spin_unlock(&dcache_lock);
437 lookup2_finish(int flag, struct ptlrpc_request *request, struct dentry **de,
438 struct lookup_intent *it, int offset, obd_id ino)
440 struct dentry *dentry = *de, *saved = *de;
441 struct inode *inode = NULL;
442 struct ll_read_inode2_cookie lic = {.lic_body = NULL, .lic_lmm = NULL};
444 if (!(flag & LL_LOOKUP_NEGATIVE)) {
446 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
448 if (S_ISREG(lic.lic_body->mode) &&
449 lic.lic_body->valid & OBD_MD_FLEASIZE) {
450 LASSERT(request->rq_repmsg->bufcount > offset);
451 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg,
457 /* No rpc's happen during iget4, -ENOMEM's are possible */
458 inode = ll_iget(dentry->d_sb, ino, &lic);
460 /* XXX make sure that request is freed in this case;
461 * I think it is, but double-check refcounts. -phil */
465 dentry = *de = ll_find_alias(inode, dentry);
467 /* We asked for a lock on the directory, and may have been
468 * granted a lock on the inode. Just in case, fixup the data
470 mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
476 ptlrpc_req_finished(request);
478 dentry->d_op = &ll_d_ops;
482 d_add(dentry, inode);
487 static struct dentry *ll_lookup2(struct inode *parent, struct dentry *dentry,
488 struct lookup_intent *it)
490 struct dentry *save = dentry;
494 if (it && it->it_op == IT_TRUNC)
495 it->it_op = IT_SETATTR;
497 rc = ll_intent_lock(parent, &dentry, it, lookup2_finish);
499 CDEBUG(D_INFO, "ll_intent_lock: %d\n", rc);
509 /* We depend on "mode" being set with the proper file type/umask by now */
510 static struct inode *ll_create_node(struct inode *dir, const char *name,
511 int namelen, const void *data, int datalen,
512 int mode, __u64 extra,
513 struct lookup_intent *it)
516 struct ptlrpc_request *request = NULL;
517 struct mds_body *body;
518 time_t time = CURRENT_TIME;
519 struct ll_sb_info *sbi = ll_i2sbi(dir);
520 struct ll_read_inode2_cookie lic = { .lic_lmm = NULL, };
523 if (it && it->it_disposition) {
524 ll_invalidate_inode_pages(dir);
525 request = it->it_data;
526 body = lustre_msg_buf(request->rq_repmsg, 1);
528 int gid = current->fsgid;
531 if (dir->i_mode & S_ISGID) {
537 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen,
538 data, datalen, mode, current->fsuid, gid,
539 time, extra, &request);
544 body = lustre_msg_buf(request->rq_repmsg, 0);
549 inode = ll_iget(dir->i_sb, body->ino, &lic);
551 int rc = PTR_ERR(inode);
552 CERROR("new_inode -fatal: rc %d\n", rc);
557 if (!list_empty(&inode->i_dentry)) {
558 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
559 body->ino, atomic_read(&inode->i_count),
563 inode = ERR_PTR(-EIO);
567 if (it && it->it_disposition) {
568 /* We asked for a lock on the directory, but were
569 * granted a lock on the inode. Since we finally have
570 * an inode pointer, stuff it in the lock. */
571 mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
577 ptlrpc_req_finished(request);
581 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
582 const char *name, int len)
584 struct ptlrpc_request *request = NULL;
585 struct ll_sb_info *sbi = ll_i2sbi(dir);
586 struct mds_body *body;
587 struct lov_stripe_md *lsm = NULL;
588 struct lustre_handle lockh;
589 struct lookup_intent it = { .it_op = IT_UNLINK };
592 struct mdc_unlink_data data;
597 data.unl_mode = mode;
598 data.unl_name = name;
601 err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_PLAIN, &it, LCK_EX, dir,
602 NULL, &lockh, NULL, 0, &data, sizeof(data));
603 mdc_put_rpc_lock(&mdc_rpc_lock, &it);
604 request = (struct ptlrpc_request *)it.it_data;
608 GOTO(out, err = it.it_status);
611 body = lustre_msg_buf(request->rq_repmsg, 1);
612 LASSERT(body != NULL);
613 if (!(body->valid & OBD_MD_FLEASIZE))
616 /* The MDS sent back the EA because we unlinked the last reference
617 * to this file. Use this EA to unlink the objects on the OST */
618 err = obd_unpackmd(ll_i2obdconn(dir), &lsm,
619 lustre_msg_buf(request->rq_repmsg, 2));
621 CERROR("obd_unpackmd: %d\n", err);
625 GOTO(out_unlock, err = -ENOMEM);
627 oa->o_id = lsm->lsm_object_id;
628 oa->o_mode = body->mode & S_IFMT;
629 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
631 err = obd_destroy(ll_i2obdconn(dir), oa, lsm, NULL);
634 CERROR("obd destroy objid 0x"LPX64" error %d\n",
635 lsm->lsm_object_id, err);
637 obd_free_memmd(ll_i2obdconn(dir), &lsm);
639 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
641 ptlrpc_req_finished(request);
646 * By the time this is called, we already have created the directory cache
647 * entry for the new file, but it is so far negative - it has no inode.
649 * We defer creating the OBD object(s) until open, to keep the intent and
650 * non-intent code paths similar, and also because we do not have the MDS
651 * inode number before calling ll_create_node() (which is needed for LOV),
652 * so we would need to do yet another RPC to the MDS to store the LOV EA
653 * data on the MDS. If needed, we would pass the PACKED lmm as data and
654 * lmm_size in datalen (the MDS still has code which will handle that).
656 * If the create succeeds, we fill in the inode information
657 * with d_instantiate().
659 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
661 struct lookup_intent *it;
668 rc = ll_it_open_error(IT_OPEN_CREATE, it);
670 LL_GET_INTENT(dentry, it);
671 ptlrpc_req_finished(it->it_data);
675 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
676 NULL, 0, mode, 0, it);
679 LL_GET_INTENT(dentry, it);
680 RETURN(PTR_ERR(inode));
683 /* no directory data updates when intents rule */
684 if (it && it->it_disposition) {
685 d_instantiate(dentry, inode);
689 rc = ext2_add_nondir(dentry, inode);
693 static int ll_mknod2(struct inode *dir, const char *name, int len, int mode,
696 struct ptlrpc_request *request = NULL;
697 time_t time = CURRENT_TIME;
698 struct ll_sb_info *sbi = ll_i2sbi(dir);
702 if (dir->i_nlink >= EXT2_LINK_MAX)
705 mode &= ~current->fs->umask;
707 switch (mode & S_IFMT) {
708 case 0: case S_IFREG:
709 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
710 case S_IFCHR: case S_IFBLK:
711 case S_IFIFO: case S_IFSOCK:
712 err = mdc_create(&sbi->ll_mdc_conn, dir, name, len, NULL, 0,
713 mode, current->fsuid, current->fsgid, time,
715 ptlrpc_req_finished(request);
726 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
729 struct lookup_intent *it;
733 LL_GET_INTENT(dentry, it);
735 if ((mode & S_IFMT) == 0)
737 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
738 NULL, 0, mode, rdev, it);
741 RETURN(PTR_ERR(inode));
743 /* no directory data updates when intents rule */
744 if (it && it->it_disposition)
745 d_instantiate(dentry, inode);
747 rc = ext2_add_nondir(dentry, inode);
752 static int ll_symlink2(struct inode *dir, const char *name, int len,
755 struct ptlrpc_request *request = NULL;
756 time_t time = CURRENT_TIME;
757 struct ll_sb_info *sbi = ll_i2sbi(dir);
761 if (dir->i_nlink >= EXT2_LINK_MAX)
764 err = mdc_create(&sbi->ll_mdc_conn, dir, name, len,
765 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
766 current->fsuid, current->fsgid, time, 0, &request);
767 ptlrpc_req_finished(request);
771 static int ll_symlink(struct inode *dir, struct dentry *dentry,
774 struct lookup_intent *it;
775 unsigned l = strlen(symname) + 1;
777 struct ll_inode_info *lli;
781 LL_GET_INTENT(dentry, it);
783 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
784 symname, l, S_IFLNK | S_IRWXUGO, 0, it);
786 RETURN(PTR_ERR(inode));
788 lli = ll_i2info(inode);
790 OBD_ALLOC(lli->lli_symlink_name, l);
791 /* this _could_ be a non-fatal error, since the symlink is already
792 * stored on the MDS by this point, and we can re-get it in readlink.
794 if (!lli->lli_symlink_name)
797 memcpy(lli->lli_symlink_name, symname, l);
798 inode->i_size = l - 1;
800 /* no directory data updates when intents rule */
801 if (it && it->it_disposition)
802 d_instantiate(dentry, inode);
804 err = ext2_add_nondir(dentry, inode);
809 static int ll_link2(struct inode *src, struct inode *dir,
810 const char *name, int len)
812 struct ptlrpc_request *request = NULL;
814 struct ll_sb_info *sbi = ll_i2sbi(dir);
818 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
819 ptlrpc_req_finished(request);
824 static int ll_link(struct dentry *old_dentry, struct inode * dir,
825 struct dentry *dentry)
827 struct lookup_intent *it;
828 struct inode *inode = old_dentry->d_inode;
831 LL_GET_INTENT(dentry, it);
833 if (it && it->it_disposition) {
835 RETURN(it->it_status);
836 inode->i_ctime = CURRENT_TIME;
837 ext2_inc_count(inode);
838 atomic_inc(&inode->i_count);
839 d_instantiate(dentry, inode);
840 ll_invalidate_inode_pages(dir);
844 if (S_ISDIR(inode->i_mode))
847 if (inode->i_nlink >= EXT2_LINK_MAX)
850 rc = ll_link2(old_dentry->d_inode, dir,
851 dentry->d_name.name, dentry->d_name.len);
855 inode->i_ctime = CURRENT_TIME;
856 ext2_inc_count(inode);
857 atomic_inc(&inode->i_count);
859 return ext2_add_nondir(dentry, inode);
862 static int ll_mkdir2(struct inode *dir, const char *name, int len, int mode)
864 struct ptlrpc_request *request = NULL;
865 time_t time = CURRENT_TIME;
866 struct ll_sb_info *sbi = ll_i2sbi(dir);
870 if (dir->i_nlink >= EXT2_LINK_MAX)
873 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
874 err = mdc_create(&sbi->ll_mdc_conn, dir, name, len, NULL, 0,
875 mode, current->fsuid, current->fsgid,
877 ptlrpc_req_finished(request);
882 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
884 struct lookup_intent *it;
885 struct inode * inode;
889 LL_GET_INTENT(dentry, it);
891 if (dir->i_nlink >= EXT2_LINK_MAX)
895 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
896 NULL, 0, S_IFDIR | mode, 0, it);
897 err = PTR_ERR(inode);
901 err = ext2_make_empty(inode, dir);
905 /* no directory data updates when intents rule */
906 if (!it || !it->it_disposition) {
907 /* XXX FIXME This code needs re-checked for non-intents */
908 ext2_inc_count(inode);
909 err = ll_add_link(dentry, inode);
914 d_instantiate(dentry, inode);
920 ext2_dec_count(inode);
921 ext2_dec_count(inode);
930 static int ll_rmdir2(struct inode *dir, const char *name, int len)
935 rc = ll_mdc_unlink(dir, NULL, S_IFDIR, name, len);
939 static int ll_unlink2(struct inode *dir, const char *name, int len)
944 rc = ll_mdc_unlink(dir, NULL, S_IFREG, name, len);
948 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
949 struct lookup_intent *it, __u32 mode)
951 struct inode *inode = dentry->d_inode;
952 struct ext2_dir_entry_2 * de;
957 if (it && it->it_disposition) {
959 ll_invalidate_inode_pages(dir);
965 de = ext2_find_entry(dir, dentry, &page);
967 GOTO(out, rc = -ENOENT);
968 rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
969 dentry->d_name.name, dentry->d_name.len);
973 rc = ext2_delete_entry(de, page);
977 /* AED: not sure if needed - directory lock revocation should do it
978 * in the case where the client has cached it for non-intent ops.
980 ll_invalidate_inode_pages(dir);
982 inode->i_ctime = dir->i_ctime;
985 ext2_dec_count(inode);
990 static int ll_unlink(struct inode *dir, struct dentry *dentry)
992 struct lookup_intent * it;
995 LL_GET_INTENT(dentry, it);
997 RETURN(ll_common_unlink(dir, dentry, it, S_IFREG));
1000 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1002 struct inode * inode = dentry->d_inode;
1003 struct lookup_intent *it;
1007 LL_GET_INTENT(dentry, it);
1009 if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
1012 rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
1015 ext2_dec_count(inode);
1016 ext2_dec_count(dir);
1022 static int ll_rename2(struct inode *src, struct inode *tgt,
1023 const char *oldname, int oldlen,
1024 const char *newname, int newlen)
1026 struct ptlrpc_request *request = NULL;
1027 struct ll_sb_info *sbi = ll_i2sbi(src);
1031 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
1032 oldname, oldlen, newname, newlen, &request);
1033 ptlrpc_req_finished(request);
1040 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
1041 struct inode * new_dir, struct dentry * new_dentry)
1043 struct lookup_intent *it;
1044 struct inode * old_inode = old_dentry->d_inode;
1045 struct inode * tgt_inode = new_dentry->d_inode;
1046 struct page * dir_page = NULL;
1047 struct ext2_dir_entry_2 * dir_de = NULL;
1048 struct ext2_dir_entry_2 * old_de;
1049 struct page * old_page;
1052 LL_GET_INTENT(new_dentry, it);
1054 if (it && it->it_disposition) {
1056 tgt_inode->i_ctime = CURRENT_TIME;
1057 tgt_inode->i_nlink--;
1059 ll_invalidate_inode_pages(old_dir);
1060 ll_invalidate_inode_pages(new_dir);
1061 GOTO(out, err = it->it_status);
1064 err = ll_rename2(old_dir, new_dir,
1065 old_dentry->d_name.name, old_dentry->d_name.len,
1066 new_dentry->d_name.name, new_dentry->d_name.len);
1070 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
1074 if (S_ISDIR(old_inode->i_mode)) {
1076 dir_de = ext2_dotdot(old_inode, &dir_page);
1082 struct page *new_page;
1083 struct ext2_dir_entry_2 *new_de;
1086 if (dir_de && !ext2_empty_dir (tgt_inode))
1090 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
1093 ext2_inc_count(old_inode);
1094 ext2_set_link(new_dir, new_de, new_page, old_inode);
1095 tgt_inode->i_ctime = CURRENT_TIME;
1097 tgt_inode->i_nlink--;
1098 ext2_dec_count(tgt_inode);
1102 if (new_dir->i_nlink >= EXT2_LINK_MAX)
1105 ext2_inc_count(old_inode);
1106 err = ll_add_link(new_dentry, old_inode);
1108 ext2_dec_count(old_inode);
1112 ext2_inc_count(new_dir);
1115 ext2_delete_entry (old_de, old_page);
1116 ext2_dec_count(old_inode);
1119 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
1120 ext2_dec_count(old_dir);
1127 page_cache_release(dir_page);
1131 page_cache_release(old_page);
1136 extern int ll_inode_revalidate(struct dentry *dentry);
1137 struct inode_operations ll_dir_inode_operations = {
1139 lookup2: ll_lookup2,
1143 unlink2: ll_unlink2,
1144 symlink: ll_symlink,
1145 symlink2: ll_symlink2,
1153 rename2: ll_rename2,
1154 setattr: ll_setattr,
1155 revalidate: ll_inode_revalidate,