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);
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);
110 extern struct dentry_operations ll_d_ops;
112 int ll_lock(struct inode *dir, struct dentry *dentry,
113 struct lookup_intent *it, struct lustre_handle *lockh)
115 struct ll_sb_info *sbi = ll_i2sbi(dir);
120 /* CREAT needs to be tested before open (both could be set) */
121 if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD))) {
123 } else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
124 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
125 IT_LINK | IT_LINK2 | IT_LOOKUP)) {
126 /* XXXphil PW for LINK2/RENAME2? */
128 } else if (it->it_op & IT_SYMLINK) {
131 tgtlen = strlen(tgt);
138 err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode,
139 dir, dentry, lockh, tgt, tgtlen, dir, sizeof(*dir));
144 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
148 ldlm_lock_decref(lockh, mode);
153 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
154 extern int ll_read_inode2(struct inode *inode, void *opaque);
155 struct inode *ll_iget(struct super_block *sb, ino_t hash,
156 struct ll_read_inode2_cookie *lic)
160 inode = iget5_locked(sb, hash, ll_test_inode, ll_read_inode2, lic);
163 return ERR_PTR(-ENOMEM);
165 if (inode->i_state & I_NEW) {
167 unlock_new_inode(inode);
170 // XXX Coda always fills inodes, should Lustre?
174 struct inode *ll_iget(struct super_block *sb, ino_t hash,
175 struct ll_read_inode2_cookie *lic)
178 inode = iget4(sb, hash, ll_find_inode, lic);
183 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
184 struct lookup_intent *it)
186 struct ptlrpc_request *request = NULL;
187 struct inode * inode = NULL;
188 struct ll_sb_info *sbi = ll_i2sbi(dir);
189 struct ll_read_inode2_cookie lic;
190 struct lustre_handle lockh;
191 struct lookup_intent lookup_it = { IT_LOOKUP };
200 CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
201 dentry->d_name.name, ldlm_it2str(it->it_op));
203 if (dentry->d_name.len > EXT2_NAME_LEN)
204 RETURN(ERR_PTR(-ENAMETOOLONG));
206 rc = ll_lock(dir, dentry, it, &lockh);
209 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
211 request = (struct ptlrpc_request *)it->it_data;
212 if (it->it_disposition) {
213 int mode, symlen = 0;
217 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
218 ino = lic.lic_body->fid1.id;
219 mode = lic.lic_body->mode;
220 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
221 /* For create ops, we want the lookup to be negative,
222 * unless the create failed in a way that indicates
223 * that the file is already there */
224 if (it->it_status != -EEXIST)
225 GOTO(negative, NULL);
226 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
228 /* For check ops, we want the lookup to succeed */
232 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
233 /* For rename, we want the source lookup to succeed */
236 GOTO(drop_req, rc = it->it_status);
238 it->it_data = dentry;
239 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
240 /* For remove ops, we want the lookup to succeed unless
241 * the file truly doesn't exist */
243 if (it->it_status == -ENOENT)
246 } else if (it->it_op == IT_OPEN) {
248 if (it->it_status && it->it_status != -EEXIST)
250 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
251 struct mds_body *body =
252 lustre_msg_buf(request->rq_repmsg, offset);
254 /* This means the target lookup is negative */
255 if (body->valid == 0)
260 /* Do a getattr now that we have the lock */
261 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
262 if (it->it_op == IT_READLINK) {
263 valid |= OBD_MD_LINKNAME;
264 symlen = lic.lic_body->size;
266 ptlrpc_req_finished(request);
268 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
269 valid, symlen, &request);
271 CERROR("failure %d inode "LPX64"\n", rc, ino);
272 GOTO(drop_req, rc = -abs(rc));
276 struct ll_inode_info *lli = ll_i2info(dir);
279 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
282 ino = ll_inode_by_name(dir, dentry, &mode);
284 CERROR("inode %*s not found by name\n",
285 dentry->d_name.len, dentry->d_name.name);
286 GOTO(drop_lock, rc = -ENOENT);
289 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
290 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
292 CERROR("failure %d inode "LPX64"\n", rc, ino);
293 GOTO(drop_req, rc = -abs(rc));
298 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
299 if (S_ISREG(lic.lic_body->mode) &&
300 lic.lic_body->valid & OBD_MD_FLEASIZE) {
301 LASSERT(request->rq_repmsg->bufcount > offset);
302 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
306 /* No rpc's happen during iget4, -ENOMEM's are possible */
308 inode = ll_iget(dir->i_sb, ino, &lic);
310 ptlrpc_free_req(request);
311 ll_intent_release(dentry, it);
312 RETURN(ERR_PTR(-ENOMEM));
317 ptlrpc_req_finished(request);
319 dentry->d_op = &ll_d_ops;
320 if (ll_d2d(dentry) == NULL) {
323 CERROR("NOT allocating fsdata - already set\n");
325 d_add(dentry, inode);
327 if (it->it_status == 0) {
328 LL_SAVE_INTENT(dentry, it);
332 "D_IT dentry %p fsdata %p intent: %s status %d\n",
333 dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
337 if (it->it_op == IT_LOOKUP)
338 ll_intent_release(dentry, it);
343 ptlrpc_free_req(request);
345 #warning FIXME: must release lock here
349 static struct inode *ll_create_node(struct inode *dir, const char *name,
350 int namelen, const char *tgt, int tgtlen,
351 int mode, __u64 extra,
352 struct lookup_intent *it,
353 struct lov_stripe_md *lsm)
356 struct ptlrpc_request *request = NULL;
357 struct mds_body *body;
358 time_t time = CURRENT_TIME;
359 struct ll_sb_info *sbi = ll_i2sbi(dir);
360 struct ll_read_inode2_cookie lic;
361 struct lov_mds_md *lmm = NULL;
364 if (it && it->it_disposition) {
365 int rc = it->it_status;
367 CERROR("error creating MDS inode for %*s: rc = %d\n",
371 ll_invalidate_inode_pages(dir);
372 request = it->it_data;
373 body = lustre_msg_buf(request->rq_repmsg, 1);
376 int gid = current->fsgid;
380 OBD_ALLOC(lmm, lsm->lsm_mds_easize);
382 RETURN(ERR_PTR(-ENOMEM));
383 lov_packmd(lmm, lsm);
388 if (dir->i_mode & S_ISGID) {
394 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
395 tgtlen, mode, current->fsuid, gid,
396 time, extra, lsm, &request);
401 body = lustre_msg_buf(request->rq_repmsg, 0);
406 LASSERT(body->ino != 0);
407 inode = ll_iget(dir->i_sb, body->ino, &lic);
409 int rc = PTR_ERR(inode);
410 CERROR("new_inode -fatal: rc %d\n", rc);
415 if (!list_empty(&inode->i_dentry)) {
416 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
417 body->ino, atomic_read(&inode->i_count),
421 inode = ERR_PTR(-EIO);
428 OBD_FREE(lmm, lsm->lsm_mds_easize);
429 ptlrpc_req_finished(request);
433 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
434 const char *name, int len)
436 struct ptlrpc_request *request = NULL;
437 struct ll_sb_info *sbi = ll_i2sbi(dir);
442 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
444 ptlrpc_req_finished(request);
449 int ll_mdc_link(struct dentry *src, struct inode *dir,
450 const char *name, int len)
452 struct ptlrpc_request *request = NULL;
454 struct ll_sb_info *sbi = ll_i2sbi(dir);
458 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
459 ptlrpc_req_finished(request);
464 int ll_mdc_rename(struct inode *src, struct inode *tgt,
465 struct dentry *old, struct dentry *new)
467 struct ptlrpc_request *request = NULL;
468 struct ll_sb_info *sbi = ll_i2sbi(src);
473 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
474 old->d_name.name, old->d_name.len,
475 new->d_name.name, new->d_name.len, &request);
476 ptlrpc_req_finished(request);
482 * By the time this is called, we already have created the directory cache
483 * entry for the new file, but it is so far negative - it has no inode.
484 * We defer creating the OBD object(s) until open, to keep the intent and
485 * non-intent code paths similar, and also because we do not have the MDS
486 * inode number before calling ll_create_node() (which is needed for LOV),
487 * so we would need to do yet another RPC to the MDS to store the LOV EA
490 * If the create succeeds, we fill in the inode information
491 * with d_instantiate().
493 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
495 struct lookup_intent *it;
500 LL_GET_INTENT(dentry, it);
502 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
503 NULL, 0, mode, 0, it, NULL);
506 RETURN(PTR_ERR(inode));
508 if (it->it_disposition) {
509 struct ll_inode_info *lli = ll_i2info(inode);
510 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
511 sizeof(lli->lli_intent_lock_handle));
512 d_instantiate(dentry, inode);
514 /* no directory data updates when intents rule */
515 rc = ext2_add_nondir(dentry, inode);
521 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
524 struct lookup_intent *it;
528 LL_GET_INTENT(dentry, it);
530 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
531 NULL, 0, mode, rdev, it, NULL);
534 RETURN(PTR_ERR(inode));
536 /* no directory data updates when intents rule */
537 if (it && it->it_disposition)
538 d_instantiate(dentry, inode);
540 rc = ext2_add_nondir(dentry, inode);
545 static int ll_symlink(struct inode *dir, struct dentry *dentry,
548 struct lookup_intent *it;
549 unsigned l = strlen(symname);
551 struct ll_inode_info *lli;
555 LL_GET_INTENT(dentry, it);
557 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
558 symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
560 RETURN(PTR_ERR(inode));
562 lli = ll_i2info(inode);
564 OBD_ALLOC(lli->lli_symlink_name, l + 1);
565 /* this _could_ be a non-fatal error, since the symlink is already
566 * stored on the MDS by this point, and we can re-get it in readlink.
568 if (!lli->lli_symlink_name)
571 memcpy(lli->lli_symlink_name, symname, l + 1);
574 /* no directory data updates when intents rule */
575 if (it && it->it_disposition)
576 d_instantiate(dentry, inode);
578 err = ext2_add_nondir(dentry, inode);
583 static int ll_link(struct dentry *old_dentry, struct inode * dir,
584 struct dentry *dentry)
586 struct lookup_intent *it;
587 struct inode *inode = old_dentry->d_inode;
590 LL_GET_INTENT(dentry, it);
592 if (it && it->it_disposition) {
594 RETURN(it->it_status);
595 inode->i_ctime = CURRENT_TIME;
596 ext2_inc_count(inode);
597 atomic_inc(&inode->i_count);
598 d_instantiate(dentry, inode);
599 ll_invalidate_inode_pages(dir);
603 if (S_ISDIR(inode->i_mode))
606 if (inode->i_nlink >= EXT2_LINK_MAX)
609 rc = ll_mdc_link(old_dentry, dir,
610 dentry->d_name.name, dentry->d_name.len);
614 inode->i_ctime = CURRENT_TIME;
615 ext2_inc_count(inode);
616 atomic_inc(&inode->i_count);
618 return ext2_add_nondir(dentry, inode);
621 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
623 struct lookup_intent *it;
624 struct inode * inode;
628 LL_GET_INTENT(dentry, it);
630 if (dir->i_nlink >= EXT2_LINK_MAX)
634 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
635 NULL, 0, S_IFDIR | mode, 0, it, NULL);
636 err = PTR_ERR(inode);
640 ext2_inc_count(inode);
642 err = ext2_make_empty(inode, dir);
646 /* no directory data updates when intents rule */
647 if (!it || !it->it_disposition) {
648 err = ll_add_link(dentry, inode);
653 d_instantiate(dentry, inode);
659 ext2_dec_count(inode);
660 ext2_dec_count(inode);
669 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
670 struct lookup_intent *it, __u32 mode)
672 struct inode *inode = dentry->d_inode;
673 struct ext2_dir_entry_2 * de;
677 if (it && it->it_disposition) {
679 ll_invalidate_inode_pages(dir);
685 de = ext2_find_entry(dir, dentry, &page);
687 GOTO(out, rc = -ENOENT);
688 rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
689 dentry->d_name.name, dentry->d_name.len);
693 rc = ext2_delete_entry(de, page);
697 /* AED: not sure if needed - directory lock revocation should do it
698 * in the case where the client has cached it for non-intent ops.
700 ll_invalidate_inode_pages(dir);
702 inode->i_ctime = dir->i_ctime;
704 ext2_dec_count(inode);
709 static int ll_unlink(struct inode *dir, struct dentry *dentry)
711 struct lookup_intent * it;
713 LL_GET_INTENT(dentry, it);
715 return ll_common_unlink(dir, dentry, it, S_IFREG);
718 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
720 struct inode * inode = dentry->d_inode;
721 struct lookup_intent *it;
725 LL_GET_INTENT(dentry, it);
727 if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
730 rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
733 ext2_dec_count(inode);
740 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
741 struct inode * new_dir, struct dentry * new_dentry)
743 struct lookup_intent *it;
744 struct inode * old_inode = old_dentry->d_inode;
745 struct inode * tgt_inode = new_dentry->d_inode;
746 struct page * dir_page = NULL;
747 struct ext2_dir_entry_2 * dir_de = NULL;
748 struct ext2_dir_entry_2 * old_de;
749 struct page * old_page;
752 LL_GET_INTENT(new_dentry, it);
754 if (it && it->it_disposition) {
756 tgt_inode->i_ctime = CURRENT_TIME;
757 tgt_inode->i_nlink--;
759 ll_invalidate_inode_pages(old_dir);
760 ll_invalidate_inode_pages(new_dir);
761 GOTO(out, err = it->it_status);
764 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
768 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
772 if (S_ISDIR(old_inode->i_mode)) {
774 dir_de = ext2_dotdot(old_inode, &dir_page);
780 struct page *new_page;
781 struct ext2_dir_entry_2 *new_de;
784 if (dir_de && !ext2_empty_dir (tgt_inode))
788 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
791 ext2_inc_count(old_inode);
792 ext2_set_link(new_dir, new_de, new_page, old_inode);
793 tgt_inode->i_ctime = CURRENT_TIME;
795 tgt_inode->i_nlink--;
796 ext2_dec_count(tgt_inode);
800 if (new_dir->i_nlink >= EXT2_LINK_MAX)
803 ext2_inc_count(old_inode);
804 err = ll_add_link(new_dentry, old_inode);
806 ext2_dec_count(old_inode);
810 ext2_inc_count(new_dir);
813 ext2_delete_entry (old_de, old_page);
814 ext2_dec_count(old_inode);
817 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
818 ext2_dec_count(old_dir);
825 page_cache_release(dir_page);
829 page_cache_release(old_page);
834 struct inode_operations ll_dir_inode_operations = {