1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * This code is issued under the GNU General Public License.
5 * See the file COPYING in this distribution
7 * Copyright (C) 1992, 1993, 1994, 1995
8 * Remy Card (card@masi.ibp.fr)
9 * Laboratoire MASI - Institut Blaise Pascal
10 * Universite Pierre et Marie Curie (Paris VI)
14 * linux/fs/ext2/namei.c
16 * Copyright (C) 1991, 1992 Linus Torvalds
18 * Big-endian to little-endian byte-swapping/bitmaps by
19 * David S. Miller (davem@caip.rutgers.edu), 1995
20 * Directory entry file type support and forward compatibility hooks
21 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
23 * Changes for use in OBDFS
24 * Copyright (c) 1999, Seagate Technology Inc.
25 * Copyright (C) 2001, Cluster File Systems, Inc.
26 * Rewritten based on recent ext2 page cache use.
31 #include <linux/sched.h>
33 #include <linux/smp_lock.h>
34 #include <linux/quotaops.h>
35 #include <linux/highmem.h>
36 #include <linux/pagemap.h>
38 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <linux/obd_support.h>
41 #include <linux/lustre_lite.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/obd_lov.h>
45 extern struct address_space_operations ll_aops;
48 extern void ll_change_inode(struct inode *inode);
49 extern int ll_setattr(struct dentry *de, struct iattr *attr);
52 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
53 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
54 int ext2_make_empty(struct inode *inode, struct inode *parent);
55 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
56 struct dentry *dentry, struct page ** res_page);
57 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
58 int ext2_empty_dir (struct inode * inode);
59 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
60 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
61 struct page *page, struct inode *inode);
64 * Couple of helper functions - make the code slightly cleaner.
66 static inline void ext2_inc_count(struct inode *inode)
71 /* postpone the disk update until the inode really goes away */
72 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);
91 static int ll_test_inode(struct inode *inode, void *opaque)
93 struct ll_read_inode2_cookie *lic = opaque;
94 struct mds_body *body = lic->lic_body;
96 if (inode->i_generation != lic->lic_body->generation)
99 /* Apply the attributes in 'opaque' to this inode */
100 ll_update_inode(inode, body);
105 extern struct dentry_operations ll_d_ops;
107 int ll_lock(struct inode *dir, struct dentry *dentry,
108 struct lookup_intent *it, struct lustre_handle *lockh)
110 struct ll_sb_info *sbi = ll_i2sbi(dir);
115 /* CREAT needs to be tested before open (both could be set) */
116 if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD))) {
118 } else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
119 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
120 IT_LINK | IT_LINK2 | IT_LOOKUP)) {
121 /* XXXphil PW for LINK2/RENAME2? */
123 } else if (it->it_op & IT_SYMLINK) {
126 tgtlen = strlen(tgt);
133 err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode,
134 dir, dentry, lockh, tgt, tgtlen, dir, sizeof(*dir));
139 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
143 ldlm_lock_decref(lockh, mode);
148 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
149 extern int ll_read_inode2(struct inode *inode, void *opaque);
150 struct inode *ll_iget(struct super_block *sb, ino_t hash,
151 struct ll_read_inode2_cookie *lic)
155 inode = iget5_locked(sb, hash, ll_test_inode, ll_read_inode2, lic);
158 return ERR_PTR(-ENOMEM);
160 if (inode->i_state & I_NEW) {
162 unlock_new_inode(inode);
165 // XXX Coda always fills inodes, should Lustre?
169 struct inode *ll_iget(struct super_block *sb, inot_t hash,
170 struct ll_read_inode2_cookie *lic)
173 inode = iget4(sb, hash, ll_find_inode, lic);
178 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
179 struct lookup_intent *it)
181 struct ptlrpc_request *request = NULL;
182 struct inode * inode = NULL;
183 struct ll_sb_info *sbi = ll_i2sbi(dir);
184 struct ll_read_inode2_cookie lic;
185 struct lustre_handle lockh;
186 struct lookup_intent lookup_it = { IT_LOOKUP };
192 /* CHECK_MOUNT_EPOCH(dir); */
193 if (ll_i2info(dir)->lli_mount_epoch != ll_i2sbi(dir)->ll_mount_epoch) {
195 RETURN(ERR_PTR(-EIO));
201 CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
202 dentry->d_name.name, ldlm_it2str(it->it_op));
204 if (dentry->d_name.len > EXT2_NAME_LEN)
205 RETURN(ERR_PTR(-ENAMETOOLONG));
207 rc = ll_lock(dir, dentry, it, &lockh);
210 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
212 request = (struct ptlrpc_request *)it->it_data;
213 if (it->it_disposition) {
214 int mode, symlen = 0;
218 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
219 ino = lic.lic_body->fid1.id;
220 mode = lic.lic_body->mode;
221 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
222 /* For create ops, we want the lookup to be negative,
223 * unless the create failed in a way that indicates
224 * that the file is already there */
225 if (it->it_status != -EEXIST)
226 GOTO(negative, NULL);
227 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
229 /* For check ops, we want the lookup to succeed */
233 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
234 /* For rename, we want the source lookup to succeed */
237 GOTO(drop_req, rc = it->it_status);
239 it->it_data = dentry;
240 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
241 /* For remove ops, we want the lookup to succeed unless
242 * the file truly doesn't exist */
244 if (it->it_status == -ENOENT)
247 } else if (it->it_op == IT_OPEN) {
249 if (it->it_status && it->it_status != -EEXIST)
251 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
252 struct mds_body *body =
253 lustre_msg_buf(request->rq_repmsg, offset);
255 /* This means the target lookup is negative */
256 if (body->valid == 0)
261 /* Do a getattr now that we have the lock */
262 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
263 if (it->it_op == IT_READLINK) {
264 valid |= OBD_MD_LINKNAME;
265 symlen = lic.lic_body->size;
267 ptlrpc_req_finished(request);
269 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
270 valid, symlen, &request);
272 CERROR("failure %d inode "LPX64"\n", rc, ino);
273 GOTO(drop_req, rc = -abs(rc));
277 struct ll_inode_info *lli = ll_i2info(dir);
280 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
283 ino = ll_inode_by_name(dir, dentry, &mode);
285 CERROR("inode %*s not found by name\n",
286 dentry->d_name.len, dentry->d_name.name);
287 GOTO(drop_lock, rc = -ENOENT);
290 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
291 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
293 CERROR("failure %d inode "LPX64"\n", rc, ino);
294 GOTO(drop_req, rc = -abs(rc));
299 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
300 if (S_ISREG(lic.lic_body->mode) &&
301 lic.lic_body->valid & OBD_MD_FLEASIZE) {
302 LASSERT(request->rq_repmsg->bufcount > offset);
303 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
307 /* No rpc's happen during iget4, -ENOMEM's are possible */
309 inode = ll_iget(dir->i_sb, ino, &lic);
311 ptlrpc_free_req(request);
312 ll_intent_release(dentry, it);
313 RETURN(ERR_PTR(-ENOMEM));
318 ptlrpc_req_finished(request);
320 dentry->d_op = &ll_d_ops;
321 if (ll_d2d(dentry) == NULL) {
324 CERROR("NOT allocating fsdata - already set\n");
326 d_add(dentry, inode);
328 if (it->it_status == 0) {
329 LL_SAVE_INTENT(dentry, it);
334 "D_IT dentry %p fsdata %p intent: %s status %d\n",
335 dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
339 if (it->it_op == IT_LOOKUP)
340 ll_intent_release(dentry, it);
345 ptlrpc_free_req(request);
347 #warning FIXME: must release lock here
351 static struct inode *ll_create_node(struct inode *dir, const char *name,
352 int namelen, const char *tgt, int tgtlen,
353 int mode, __u64 extra,
354 struct lookup_intent *it,
355 struct lov_stripe_md *lsm)
358 struct ptlrpc_request *request = NULL;
359 struct mds_body *body;
360 time_t time = CURRENT_TIME;
361 struct ll_sb_info *sbi = ll_i2sbi(dir);
362 struct ll_read_inode2_cookie lic;
363 struct lov_mds_md *lmm = NULL;
366 if (it && it->it_disposition) {
367 int rc = it->it_status;
369 CERROR("error creating MDS inode for %*s: rc = %d\n",
373 invalidate_inode_pages(dir);
374 request = it->it_data;
375 body = lustre_msg_buf(request->rq_repmsg, 1);
378 int gid = current->fsgid;
382 OBD_ALLOC(lmm, lsm->lsm_mds_easize);
384 RETURN(ERR_PTR(-ENOMEM));
385 lov_packmd(lmm, lsm);
390 if (dir->i_mode & S_ISGID) {
396 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
397 tgtlen, mode, current->fsuid, gid,
398 time, extra, lsm, &request);
403 body = lustre_msg_buf(request->rq_repmsg, 0);
408 LASSERT(body->ino != 0);
409 inode = ll_iget(dir->i_sb, body->ino, &lic);
411 int rc = PTR_ERR(inode);
412 CERROR("new_inode -fatal: rc %d\n", rc);
417 if (!list_empty(&inode->i_dentry)) {
418 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
419 body->ino, atomic_read(&inode->i_count),
423 inode = ERR_PTR(-EIO);
430 OBD_FREE(lmm, lsm->lsm_mds_easize);
431 ptlrpc_req_finished(request);
435 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
436 const char *name, int len)
438 struct ptlrpc_request *request = NULL;
439 struct ll_sb_info *sbi = ll_i2sbi(dir);
444 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
446 ptlrpc_req_finished(request);
451 int ll_mdc_link(struct dentry *src, struct inode *dir,
452 const char *name, int len)
454 struct ptlrpc_request *request = NULL;
456 struct ll_sb_info *sbi = ll_i2sbi(dir);
460 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
461 ptlrpc_req_finished(request);
466 int ll_mdc_rename(struct inode *src, struct inode *tgt,
467 struct dentry *old, struct dentry *new)
469 struct ptlrpc_request *request = NULL;
470 struct ll_sb_info *sbi = ll_i2sbi(src);
475 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
476 old->d_name.name, old->d_name.len,
477 new->d_name.name, new->d_name.len, &request);
478 ptlrpc_req_finished(request);
484 * By the time this is called, we already have created the directory cache
485 * entry for the new file, but it is so far negative - it has no inode.
486 * We defer creating the OBD object(s) until open, to keep the intent and
487 * non-intent code paths similar, and also because we do not have the MDS
488 * inode number before calling ll_create_node() (which is needed for LOV),
489 * so we would need to do yet another RPC to the MDS to store the LOV EA
492 * If the create succeeds, we fill in the inode information
493 * with d_instantiate().
495 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
497 struct lookup_intent *it;
502 CHECK_MOUNT_EPOCH(dir);
504 LL_GET_INTENT(dentry, it);
506 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
507 NULL, 0, mode, 0, it, NULL);
510 RETURN(PTR_ERR(inode));
512 if (it->it_disposition) {
513 struct ll_inode_info *lli = ll_i2info(inode);
514 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
515 sizeof(lli->lli_intent_lock_handle));
516 d_instantiate(dentry, inode);
518 /* no directory data updates when intents rule */
519 rc = ext2_add_nondir(dentry, inode);
525 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
528 struct lookup_intent *it;
532 LL_GET_INTENT(dentry, it);
534 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
535 NULL, 0, mode, rdev, it, NULL);
538 RETURN(PTR_ERR(inode));
540 /* no directory data updates when intents rule */
541 if (it && it->it_disposition)
542 d_instantiate(dentry, inode);
544 rc = ext2_add_nondir(dentry, inode);
549 static int ll_symlink(struct inode *dir, struct dentry *dentry,
552 struct lookup_intent *it;
553 unsigned l = strlen(symname);
555 struct ll_inode_info *lli;
559 CHECK_MOUNT_EPOCH(dir);
561 LL_GET_INTENT(dentry, it);
563 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
564 symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
566 RETURN(PTR_ERR(inode));
568 lli = ll_i2info(inode);
570 OBD_ALLOC(lli->lli_symlink_name, l + 1);
571 /* this _could_ be a non-fatal error, since the symlink is already
572 * stored on the MDS by this point, and we can re-get it in readlink.
574 if (!lli->lli_symlink_name)
577 memcpy(lli->lli_symlink_name, symname, l + 1);
580 /* no directory data updates when intents rule */
581 if (it && it->it_disposition)
582 d_instantiate(dentry, inode);
584 err = ext2_add_nondir(dentry, inode);
589 static int ll_link(struct dentry *old_dentry, struct inode * dir,
590 struct dentry *dentry)
592 struct lookup_intent *it;
593 struct inode *inode = old_dentry->d_inode;
596 LL_GET_INTENT(dentry, it);
598 if (it && it->it_disposition) {
600 RETURN(it->it_status);
601 inode->i_ctime = CURRENT_TIME;
602 ext2_inc_count(inode);
603 atomic_inc(&inode->i_count);
604 d_instantiate(dentry, inode);
605 invalidate_inode_pages(dir);
609 if (S_ISDIR(inode->i_mode))
612 if (inode->i_nlink >= EXT2_LINK_MAX)
615 rc = ll_mdc_link(old_dentry, dir,
616 dentry->d_name.name, dentry->d_name.len);
620 inode->i_ctime = CURRENT_TIME;
621 ext2_inc_count(inode);
622 atomic_inc(&inode->i_count);
624 return ext2_add_nondir(dentry, inode);
627 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
629 struct lookup_intent *it;
630 struct inode * inode;
634 LL_GET_INTENT(dentry, it);
636 if (dir->i_nlink >= EXT2_LINK_MAX)
640 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
641 NULL, 0, S_IFDIR | mode, 0, it, NULL);
642 err = PTR_ERR(inode);
646 ext2_inc_count(inode);
648 err = ext2_make_empty(inode, dir);
652 /* no directory data updates when intents rule */
653 if (!it || !it->it_disposition) {
654 err = ll_add_link(dentry, inode);
659 d_instantiate(dentry, inode);
665 ext2_dec_count(inode);
666 ext2_dec_count(inode);
675 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
676 struct lookup_intent *it, __u32 mode)
678 struct inode *inode = dentry->d_inode;
679 struct ext2_dir_entry_2 * de;
683 if (it && it->it_disposition) {
685 invalidate_inode_pages(dir);
691 de = ext2_find_entry(dir, dentry, &page);
693 GOTO(out, rc = -ENOENT);
694 rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
695 dentry->d_name.name, dentry->d_name.len);
699 rc = ext2_delete_entry(de, page);
703 /* AED: not sure if needed - directory lock revocation should do it
704 * in the case where the client has cached it for non-intent ops.
706 invalidate_inode_pages(dir);
708 inode->i_ctime = dir->i_ctime;
710 ext2_dec_count(inode);
715 static int ll_unlink(struct inode *dir, struct dentry *dentry)
717 struct lookup_intent * it;
719 LL_GET_INTENT(dentry, it);
721 return ll_common_unlink(dir, dentry, it, S_IFREG);
724 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
726 struct inode * inode = dentry->d_inode;
727 struct lookup_intent *it;
731 LL_GET_INTENT(dentry, it);
734 if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
737 rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
740 ext2_dec_count(inode);
747 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
748 struct inode * new_dir, struct dentry * new_dentry)
750 struct lookup_intent *it;
751 struct inode * old_inode = old_dentry->d_inode;
752 struct inode * tgt_inode = new_dentry->d_inode;
753 struct page * dir_page = NULL;
754 struct ext2_dir_entry_2 * dir_de = NULL;
755 struct ext2_dir_entry_2 * old_de;
756 struct page * old_page;
759 LL_GET_INTENT(new_dentry, it);
761 if (it && it->it_disposition) {
763 tgt_inode->i_ctime = CURRENT_TIME;
764 tgt_inode->i_nlink--;
766 invalidate_inode_pages(old_dir);
767 invalidate_inode_pages(new_dir);
768 GOTO(out, err = it->it_status);
771 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
775 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
779 if (S_ISDIR(old_inode->i_mode)) {
781 dir_de = ext2_dotdot(old_inode, &dir_page);
787 struct page *new_page;
788 struct ext2_dir_entry_2 *new_de;
791 if (dir_de && !ext2_empty_dir (tgt_inode))
795 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
798 ext2_inc_count(old_inode);
799 ext2_set_link(new_dir, new_de, new_page, old_inode);
800 tgt_inode->i_ctime = CURRENT_TIME;
802 tgt_inode->i_nlink--;
803 ext2_dec_count(tgt_inode);
807 if (new_dir->i_nlink >= EXT2_LINK_MAX)
810 ext2_inc_count(old_inode);
811 err = ll_add_link(new_dentry, old_inode);
813 ext2_dec_count(old_inode);
817 ext2_inc_count(new_dir);
820 ext2_delete_entry (old_de, old_page);
821 ext2_dec_count(old_inode);
824 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
825 ext2_dec_count(old_dir);
832 page_cache_release(dir_page);
836 page_cache_release(old_page);
841 struct inode_operations ll_dir_inode_operations = {