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/smp_lock.h>
32 #include <linux/quotaops.h>
33 #include <linux/highmem.h>
34 #include <linux/pagemap.h>
36 #define DEBUG_SUBSYSTEM S_LLITE
38 #include <linux/obd_support.h>
39 #include <linux/lustre_lite.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/obd_lov.h>
43 extern struct address_space_operations ll_aops;
46 extern void ll_change_inode(struct inode *inode);
47 extern int ll_setattr(struct dentry *de, struct iattr *attr);
50 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
51 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
52 int ext2_make_empty(struct inode *inode, struct inode *parent);
53 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
54 struct dentry *dentry, struct page ** res_page);
55 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
56 int ext2_empty_dir (struct inode * inode);
57 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
58 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
59 struct page *page, struct inode *inode);
62 * Couple of helper functions - make the code slightly cleaner.
64 static inline void ext2_inc_count(struct inode *inode)
69 /* postpone the disk update until the inode really goes away */
70 static inline void ext2_dec_count(struct inode *inode)
75 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
78 err = ll_add_link(dentry, inode);
80 d_instantiate(dentry, inode);
83 ext2_dec_count(inode);
89 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
91 struct ll_read_inode2_cookie *lic = opaque;
92 struct mds_body *body = lic->lic_body;
94 if (inode->i_generation != lic->lic_body->generation)
97 /* Apply the attributes in 'opaque' to this inode */
98 ll_update_inode(inode, body);
103 extern struct dentry_operations ll_d_ops;
105 int ll_lock(struct inode *dir, struct dentry *dentry,
106 struct lookup_intent *it, struct lustre_handle *lockh)
108 struct ll_sb_info *sbi = ll_i2sbi(dir);
113 /* CREAT needs to be tested before open (both could be set) */
114 if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD))) {
116 } else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
117 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
118 IT_LINK | IT_LINK2 | IT_LOOKUP)) {
119 /* XXXphil PW for LINK2/RENAME2? */
121 } else if (it->it_op & IT_SYMLINK) {
124 tgtlen = strlen(tgt);
131 err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode,
132 dir, dentry, lockh, tgt, tgtlen, dir, sizeof(*dir));
137 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
141 ldlm_lock_decref(lockh, mode);
146 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
147 struct lookup_intent *it)
149 struct ptlrpc_request *request = NULL;
150 struct inode * inode = NULL;
151 struct ll_sb_info *sbi = ll_i2sbi(dir);
152 struct ll_read_inode2_cookie lic;
153 struct lustre_handle lockh;
154 struct lookup_intent lookup_it = { IT_LOOKUP };
160 /* CHECK_MOUNT_EPOCH(dir); */
161 if (ll_i2info(dir)->lli_mount_epoch != ll_i2sbi(dir)->ll_mount_epoch) {
163 RETURN(ERR_PTR(-EIO));
169 CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
170 dentry->d_name.name, ldlm_it2str(it->it_op));
172 if (dentry->d_name.len > EXT2_NAME_LEN)
173 RETURN(ERR_PTR(-ENAMETOOLONG));
175 rc = ll_lock(dir, dentry, it, &lockh);
178 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
180 request = (struct ptlrpc_request *)it->it_data;
181 if (it->it_disposition) {
182 int mode, symlen = 0;
186 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
187 ino = lic.lic_body->fid1.id;
188 mode = lic.lic_body->mode;
189 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
190 /* For create ops, we want the lookup to be negative,
191 * unless the create failed in a way that indicates
192 * that the file is already there */
193 if (it->it_status != -EEXIST)
194 GOTO(negative, NULL);
195 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
197 /* For check ops, we want the lookup to succeed */
201 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
202 /* For rename, we want the source lookup to succeed */
205 GOTO(drop_req, rc = it->it_status);
207 it->it_data = dentry;
208 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
209 /* For remove ops, we want the lookup to succeed unless
210 * the file truly doesn't exist */
212 if (it->it_status == -ENOENT)
215 } else if (it->it_op == IT_OPEN) {
217 if (it->it_status && it->it_status != -EEXIST)
219 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
220 struct mds_body *body =
221 lustre_msg_buf(request->rq_repmsg, offset);
223 /* This means the target lookup is negative */
224 if (body->valid == 0)
229 /* Do a getattr now that we have the lock */
230 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
231 if (it->it_op == IT_READLINK) {
232 valid |= OBD_MD_LINKNAME;
233 symlen = lic.lic_body->size;
235 ptlrpc_req_finished(request);
237 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
238 valid, symlen, &request);
240 CERROR("failure %d inode "LPX64"\n", rc, ino);
241 GOTO(drop_req, rc = -abs(rc));
245 struct ll_inode_info *lli = ll_i2info(dir);
248 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
251 ino = ll_inode_by_name(dir, dentry, &mode);
253 CERROR("inode %*s not found by name\n",
254 dentry->d_name.len, dentry->d_name.name);
255 GOTO(drop_lock, rc = -ENOENT);
258 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
259 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
261 CERROR("failure %d inode "LPX64"\n", rc, ino);
262 GOTO(drop_req, rc = -abs(rc));
267 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
268 if (S_ISREG(lic.lic_body->mode) &&
269 lic.lic_body->valid & OBD_MD_FLEASIZE) {
270 LASSERT(request->rq_repmsg->bufcount > offset);
271 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
275 /* No rpc's happen during iget4, -ENOMEM's are possible */
277 inode = iget4(dir->i_sb, ino, ll_find_inode, &lic);
280 ptlrpc_free_req(request);
281 ll_intent_release(dentry, it);
282 RETURN(ERR_PTR(-ENOMEM));
287 ptlrpc_req_finished(request);
289 dentry->d_op = &ll_d_ops;
290 d_add(dentry, inode);
292 if (it->it_status == 0) {
293 LL_SAVE_INTENT(dentry, it);
298 "D_IT dentry %p fsdata %p intent: %s status %d\n",
299 dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
303 if (it->it_op == IT_LOOKUP)
304 ll_intent_release(dentry, it);
309 ptlrpc_free_req(request);
311 #warning FIXME: must release lock here
315 static struct inode *ll_create_node(struct inode *dir, const char *name,
316 int namelen, const char *tgt, int tgtlen,
317 int mode, __u64 extra,
318 struct lookup_intent *it,
319 struct lov_stripe_md *lsm)
322 struct ptlrpc_request *request = NULL;
323 struct mds_body *body;
324 time_t time = CURRENT_TIME;
325 struct ll_sb_info *sbi = ll_i2sbi(dir);
326 struct ll_read_inode2_cookie lic;
327 struct lov_mds_md *lmm = NULL;
330 if (it && it->it_disposition) {
331 int rc = it->it_status;
333 CERROR("error creating MDS inode for %*s: rc = %d\n",
337 invalidate_inode_pages(dir);
338 request = it->it_data;
339 body = lustre_msg_buf(request->rq_repmsg, 1);
342 int gid = current->fsgid;
346 OBD_ALLOC(lmm, lsm->lsm_mds_easize);
348 RETURN(ERR_PTR(-ENOMEM));
349 lov_packmd(lmm, lsm);
354 if (dir->i_mode & S_ISGID) {
360 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
361 tgtlen, mode, current->fsuid, gid,
362 time, extra, lsm, &request);
367 body = lustre_msg_buf(request->rq_repmsg, 0);
372 LASSERT(body->ino != 0);
373 inode = iget4(dir->i_sb, body->ino, ll_find_inode, &lic);
375 int rc = PTR_ERR(inode);
376 CERROR("new_inode -fatal: rc %d\n", rc);
381 if (!list_empty(&inode->i_dentry)) {
382 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
383 body->ino, atomic_read(&inode->i_count),
387 inode = ERR_PTR(-EIO);
394 OBD_FREE(lmm, lsm->lsm_mds_easize);
395 ptlrpc_req_finished(request);
399 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
400 const char *name, int len)
402 struct ptlrpc_request *request = NULL;
403 struct ll_sb_info *sbi = ll_i2sbi(dir);
408 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
410 ptlrpc_req_finished(request);
415 int ll_mdc_link(struct dentry *src, struct inode *dir,
416 const char *name, int len)
418 struct ptlrpc_request *request = NULL;
420 struct ll_sb_info *sbi = ll_i2sbi(dir);
424 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
425 ptlrpc_req_finished(request);
430 int ll_mdc_rename(struct inode *src, struct inode *tgt,
431 struct dentry *old, struct dentry *new)
433 struct ptlrpc_request *request = NULL;
434 struct ll_sb_info *sbi = ll_i2sbi(src);
439 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
440 old->d_name.name, old->d_name.len,
441 new->d_name.name, new->d_name.len, &request);
442 ptlrpc_req_finished(request);
448 * By the time this is called, we already have created the directory cache
449 * entry for the new file, but it is so far negative - it has no inode.
450 * We defer creating the OBD object(s) until open, to keep the intent and
451 * non-intent code paths similar, and also because we do not have the MDS
452 * inode number before calling ll_create_node() (which is needed for LOV),
453 * so we would need to do yet another RPC to the MDS to store the LOV EA
456 * If the create succeeds, we fill in the inode information
457 * with d_instantiate().
459 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
461 struct lookup_intent *it;
466 CHECK_MOUNT_EPOCH(dir);
468 LL_GET_INTENT(dentry, it);
470 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
471 NULL, 0, mode, 0, it, NULL);
474 RETURN(PTR_ERR(inode));
476 if (it->it_disposition) {
477 struct ll_inode_info *lli = ll_i2info(inode);
478 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
479 sizeof(lli->lli_intent_lock_handle));
480 d_instantiate(dentry, inode);
482 /* no directory data updates when intents rule */
483 rc = ext2_add_nondir(dentry, inode);
489 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
492 struct lookup_intent *it;
496 LL_GET_INTENT(dentry, it);
498 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
499 NULL, 0, mode, rdev, it, NULL);
502 RETURN(PTR_ERR(inode));
504 /* no directory data updates when intents rule */
505 if (it && it->it_disposition)
506 d_instantiate(dentry, inode);
508 rc = ext2_add_nondir(dentry, inode);
513 static int ll_symlink(struct inode *dir, struct dentry *dentry,
516 struct lookup_intent *it;
517 unsigned l = strlen(symname);
519 struct ll_inode_info *lli;
523 CHECK_MOUNT_EPOCH(dir);
525 LL_GET_INTENT(dentry, it);
527 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
528 symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
530 RETURN(PTR_ERR(inode));
532 lli = ll_i2info(inode);
534 OBD_ALLOC(lli->lli_symlink_name, l + 1);
535 /* this _could_ be a non-fatal error, since the symlink is already
536 * stored on the MDS by this point, and we can re-get it in readlink.
538 if (!lli->lli_symlink_name)
541 memcpy(lli->lli_symlink_name, symname, l + 1);
544 /* no directory data updates when intents rule */
545 if (it && it->it_disposition)
546 d_instantiate(dentry, inode);
548 err = ext2_add_nondir(dentry, inode);
553 static int ll_link(struct dentry *old_dentry, struct inode * dir,
554 struct dentry *dentry)
556 struct lookup_intent *it;
557 struct inode *inode = old_dentry->d_inode;
560 LL_GET_INTENT(dentry, it);
562 if (it && it->it_disposition) {
564 RETURN(it->it_status);
565 inode->i_ctime = CURRENT_TIME;
566 ext2_inc_count(inode);
567 atomic_inc(&inode->i_count);
568 d_instantiate(dentry, inode);
569 invalidate_inode_pages(dir);
573 if (S_ISDIR(inode->i_mode))
576 if (inode->i_nlink >= EXT2_LINK_MAX)
579 rc = ll_mdc_link(old_dentry, dir,
580 dentry->d_name.name, dentry->d_name.len);
584 inode->i_ctime = CURRENT_TIME;
585 ext2_inc_count(inode);
586 atomic_inc(&inode->i_count);
588 return ext2_add_nondir(dentry, inode);
591 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
593 struct lookup_intent *it;
594 struct inode * inode;
598 LL_GET_INTENT(dentry, it);
600 if (dir->i_nlink >= EXT2_LINK_MAX)
607 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
608 NULL, 0, S_IFDIR | mode, 0, it, NULL);
609 err = PTR_ERR(inode);
613 ext2_inc_count(inode);
615 err = ext2_make_empty(inode, dir);
619 /* no directory data updates when intents rule */
620 if (!it || !it->it_disposition) {
621 err = ll_add_link(dentry, inode);
626 d_instantiate(dentry, inode);
632 ext2_dec_count(inode);
633 ext2_dec_count(inode);
642 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
643 struct lookup_intent *it, __u32 mode)
645 struct inode *inode = dentry->d_inode;
646 struct ext2_dir_entry_2 * de;
650 if (it && it->it_disposition) {
652 invalidate_inode_pages(dir);
658 de = ext2_find_entry(dir, dentry, &page);
660 GOTO(out, rc = -ENOENT);
661 rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
662 dentry->d_name.name, dentry->d_name.len);
666 rc = ext2_delete_entry(de, page);
670 /* AED: not sure if needed - directory lock revocation should do it
671 * in the case where the client has cached it for non-intent ops.
673 invalidate_inode_pages(dir);
675 inode->i_ctime = dir->i_ctime;
677 ext2_dec_count(inode);
682 static int ll_unlink(struct inode *dir, struct dentry *dentry)
684 struct lookup_intent * it;
686 LL_GET_INTENT(dentry, it);
688 return ll_common_unlink(dir, dentry, it, S_IFREG);
691 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
693 struct inode * inode = dentry->d_inode;
694 struct lookup_intent *it;
698 LL_GET_INTENT(dentry, it);
701 if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
704 rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
707 ext2_dec_count(inode);
714 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
715 struct inode * new_dir, struct dentry * new_dentry)
717 struct lookup_intent *it;
718 struct inode * old_inode = old_dentry->d_inode;
719 struct inode * tgt_inode = new_dentry->d_inode;
720 struct page * dir_page = NULL;
721 struct ext2_dir_entry_2 * dir_de = NULL;
722 struct ext2_dir_entry_2 * old_de;
723 struct page * old_page;
726 LL_GET_INTENT(new_dentry, it);
728 if (it && it->it_disposition) {
730 tgt_inode->i_ctime = CURRENT_TIME;
731 tgt_inode->i_nlink--;
733 invalidate_inode_pages(old_dir);
734 invalidate_inode_pages(new_dir);
735 GOTO(out, err = it->it_status);
738 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
742 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
746 if (S_ISDIR(old_inode->i_mode)) {
748 dir_de = ext2_dotdot(old_inode, &dir_page);
754 struct page *new_page;
755 struct ext2_dir_entry_2 *new_de;
758 if (dir_de && !ext2_empty_dir (tgt_inode))
762 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
765 ext2_inc_count(old_inode);
766 ext2_set_link(new_dir, new_de, new_page, old_inode);
767 tgt_inode->i_ctime = CURRENT_TIME;
769 tgt_inode->i_nlink--;
770 ext2_dec_count(tgt_inode);
774 if (new_dir->i_nlink >= EXT2_LINK_MAX)
777 ext2_inc_count(old_inode);
778 err = ll_add_link(new_dentry, old_inode);
780 ext2_dec_count(old_inode);
784 ext2_inc_count(new_dir);
787 ext2_delete_entry (old_de, old_page);
788 ext2_dec_count(old_inode);
791 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
792 ext2_dec_count(old_dir);
799 page_cache_release(dir_page);
803 page_cache_release(old_page);
808 struct inode_operations ll_dir_inode_operations = {