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_find_inode(struct inode *inode, unsigned long ino, 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 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
149 struct lookup_intent *it)
151 struct ptlrpc_request *request = NULL;
152 struct inode * inode = NULL;
153 struct ll_sb_info *sbi = ll_i2sbi(dir);
154 struct ll_read_inode2_cookie lic;
155 struct lustre_handle lockh;
156 struct lookup_intent lookup_it = { IT_LOOKUP };
162 /* CHECK_MOUNT_EPOCH(dir); */
163 if (ll_i2info(dir)->lli_mount_epoch != ll_i2sbi(dir)->ll_mount_epoch) {
165 RETURN(ERR_PTR(-EIO));
171 CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
172 dentry->d_name.name, ldlm_it2str(it->it_op));
174 if (dentry->d_name.len > EXT2_NAME_LEN)
175 RETURN(ERR_PTR(-ENAMETOOLONG));
177 rc = ll_lock(dir, dentry, it, &lockh);
180 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
182 request = (struct ptlrpc_request *)it->it_data;
183 if (it->it_disposition) {
184 int mode, symlen = 0;
188 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
189 ino = lic.lic_body->fid1.id;
190 mode = lic.lic_body->mode;
191 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
192 /* For create ops, we want the lookup to be negative,
193 * unless the create failed in a way that indicates
194 * that the file is already there */
195 if (it->it_status != -EEXIST)
196 GOTO(negative, NULL);
197 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
199 /* For check ops, we want the lookup to succeed */
203 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
204 /* For rename, we want the source lookup to succeed */
207 GOTO(drop_req, rc = it->it_status);
209 it->it_data = dentry;
210 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
211 /* For remove ops, we want the lookup to succeed unless
212 * the file truly doesn't exist */
214 if (it->it_status == -ENOENT)
217 } else if (it->it_op == IT_OPEN) {
219 if (it->it_status && it->it_status != -EEXIST)
221 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
222 struct mds_body *body =
223 lustre_msg_buf(request->rq_repmsg, offset);
225 /* This means the target lookup is negative */
226 if (body->valid == 0)
231 /* Do a getattr now that we have the lock */
232 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
233 if (it->it_op == IT_READLINK) {
234 valid |= OBD_MD_LINKNAME;
235 symlen = lic.lic_body->size;
237 ptlrpc_req_finished(request);
239 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
240 valid, symlen, &request);
242 CERROR("failure %d inode "LPX64"\n", rc, ino);
243 GOTO(drop_req, rc = -abs(rc));
247 struct ll_inode_info *lli = ll_i2info(dir);
250 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
253 ino = ll_inode_by_name(dir, dentry, &mode);
255 CERROR("inode %*s not found by name\n",
256 dentry->d_name.len, dentry->d_name.name);
257 GOTO(drop_lock, rc = -ENOENT);
260 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
261 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
263 CERROR("failure %d inode "LPX64"\n", rc, ino);
264 GOTO(drop_req, rc = -abs(rc));
269 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
270 if (S_ISREG(lic.lic_body->mode) &&
271 lic.lic_body->valid & OBD_MD_FLEASIZE) {
272 LASSERT(request->rq_repmsg->bufcount > offset);
273 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
277 /* No rpc's happen during iget4, -ENOMEM's are possible */
279 inode = iget4(dir->i_sb, ino, ll_find_inode, &lic);
282 ptlrpc_free_req(request);
283 ll_intent_release(dentry, it);
284 RETURN(ERR_PTR(-ENOMEM));
289 ptlrpc_req_finished(request);
291 dentry->d_op = &ll_d_ops;
292 if (ll_d2d(dentry) == NULL) {
295 CERROR("NOT allocating fsdata - already set\n");
297 d_add(dentry, inode);
299 if (it->it_status == 0) {
300 LL_SAVE_INTENT(dentry, it);
305 "D_IT dentry %p fsdata %p intent: %s status %d\n",
306 dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
310 if (it->it_op == IT_LOOKUP)
311 ll_intent_release(dentry, it);
316 ptlrpc_free_req(request);
318 #warning FIXME: must release lock here
322 static struct inode *ll_create_node(struct inode *dir, const char *name,
323 int namelen, const char *tgt, int tgtlen,
324 int mode, __u64 extra,
325 struct lookup_intent *it,
326 struct lov_stripe_md *lsm)
329 struct ptlrpc_request *request = NULL;
330 struct mds_body *body;
331 time_t time = CURRENT_TIME;
332 struct ll_sb_info *sbi = ll_i2sbi(dir);
333 struct ll_read_inode2_cookie lic;
334 struct lov_mds_md *lmm = NULL;
337 if (it && it->it_disposition) {
338 int rc = it->it_status;
340 CERROR("error creating MDS inode for %*s: rc = %d\n",
344 invalidate_inode_pages(dir);
345 request = it->it_data;
346 body = lustre_msg_buf(request->rq_repmsg, 1);
349 int gid = current->fsgid;
353 OBD_ALLOC(lmm, lsm->lsm_mds_easize);
355 RETURN(ERR_PTR(-ENOMEM));
356 lov_packmd(lmm, lsm);
361 if (dir->i_mode & S_ISGID) {
367 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
368 tgtlen, mode, current->fsuid, gid,
369 time, extra, lsm, &request);
374 body = lustre_msg_buf(request->rq_repmsg, 0);
379 LASSERT(body->ino != 0);
380 inode = iget4(dir->i_sb, body->ino, ll_find_inode, &lic);
382 int rc = PTR_ERR(inode);
383 CERROR("new_inode -fatal: rc %d\n", rc);
388 if (!list_empty(&inode->i_dentry)) {
389 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
390 body->ino, atomic_read(&inode->i_count),
394 inode = ERR_PTR(-EIO);
401 OBD_FREE(lmm, lsm->lsm_mds_easize);
402 ptlrpc_req_finished(request);
406 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
407 const char *name, int len)
409 struct ptlrpc_request *request = NULL;
410 struct ll_sb_info *sbi = ll_i2sbi(dir);
415 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
417 ptlrpc_req_finished(request);
422 int ll_mdc_link(struct dentry *src, struct inode *dir,
423 const char *name, int len)
425 struct ptlrpc_request *request = NULL;
427 struct ll_sb_info *sbi = ll_i2sbi(dir);
431 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
432 ptlrpc_req_finished(request);
437 int ll_mdc_rename(struct inode *src, struct inode *tgt,
438 struct dentry *old, struct dentry *new)
440 struct ptlrpc_request *request = NULL;
441 struct ll_sb_info *sbi = ll_i2sbi(src);
446 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
447 old->d_name.name, old->d_name.len,
448 new->d_name.name, new->d_name.len, &request);
449 ptlrpc_req_finished(request);
455 * By the time this is called, we already have created the directory cache
456 * entry for the new file, but it is so far negative - it has no inode.
457 * We defer creating the OBD object(s) until open, to keep the intent and
458 * non-intent code paths similar, and also because we do not have the MDS
459 * inode number before calling ll_create_node() (which is needed for LOV),
460 * so we would need to do yet another RPC to the MDS to store the LOV EA
463 * If the create succeeds, we fill in the inode information
464 * with d_instantiate().
466 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
468 struct lookup_intent *it;
473 CHECK_MOUNT_EPOCH(dir);
475 LL_GET_INTENT(dentry, it);
477 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
478 NULL, 0, mode, 0, it, NULL);
481 RETURN(PTR_ERR(inode));
483 if (it->it_disposition) {
484 struct ll_inode_info *lli = ll_i2info(inode);
485 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
486 sizeof(lli->lli_intent_lock_handle));
487 d_instantiate(dentry, inode);
489 /* no directory data updates when intents rule */
490 rc = ext2_add_nondir(dentry, inode);
496 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
499 struct lookup_intent *it;
503 LL_GET_INTENT(dentry, it);
505 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
506 NULL, 0, mode, rdev, it, NULL);
509 RETURN(PTR_ERR(inode));
511 /* no directory data updates when intents rule */
512 if (it && it->it_disposition)
513 d_instantiate(dentry, inode);
515 rc = ext2_add_nondir(dentry, inode);
520 static int ll_symlink(struct inode *dir, struct dentry *dentry,
523 struct lookup_intent *it;
524 unsigned l = strlen(symname);
526 struct ll_inode_info *lli;
530 CHECK_MOUNT_EPOCH(dir);
532 LL_GET_INTENT(dentry, it);
534 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
535 symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
537 RETURN(PTR_ERR(inode));
539 lli = ll_i2info(inode);
541 OBD_ALLOC(lli->lli_symlink_name, l + 1);
542 /* this _could_ be a non-fatal error, since the symlink is already
543 * stored on the MDS by this point, and we can re-get it in readlink.
545 if (!lli->lli_symlink_name)
548 memcpy(lli->lli_symlink_name, symname, l + 1);
551 /* no directory data updates when intents rule */
552 if (it && it->it_disposition)
553 d_instantiate(dentry, inode);
555 err = ext2_add_nondir(dentry, inode);
560 static int ll_link(struct dentry *old_dentry, struct inode * dir,
561 struct dentry *dentry)
563 struct lookup_intent *it;
564 struct inode *inode = old_dentry->d_inode;
567 LL_GET_INTENT(dentry, it);
569 if (it && it->it_disposition) {
571 RETURN(it->it_status);
572 inode->i_ctime = CURRENT_TIME;
573 ext2_inc_count(inode);
574 atomic_inc(&inode->i_count);
575 d_instantiate(dentry, inode);
576 invalidate_inode_pages(dir);
580 if (S_ISDIR(inode->i_mode))
583 if (inode->i_nlink >= EXT2_LINK_MAX)
586 rc = ll_mdc_link(old_dentry, dir,
587 dentry->d_name.name, dentry->d_name.len);
591 inode->i_ctime = CURRENT_TIME;
592 ext2_inc_count(inode);
593 atomic_inc(&inode->i_count);
595 return ext2_add_nondir(dentry, inode);
598 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
600 struct lookup_intent *it;
601 struct inode * inode;
605 LL_GET_INTENT(dentry, it);
607 if (dir->i_nlink >= EXT2_LINK_MAX)
611 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
612 NULL, 0, S_IFDIR | mode, 0, it, NULL);
613 err = PTR_ERR(inode);
617 ext2_inc_count(inode);
619 err = ext2_make_empty(inode, dir);
623 /* no directory data updates when intents rule */
624 if (!it || !it->it_disposition) {
625 err = ll_add_link(dentry, inode);
630 d_instantiate(dentry, inode);
636 ext2_dec_count(inode);
637 ext2_dec_count(inode);
646 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
647 struct lookup_intent *it, __u32 mode)
649 struct inode *inode = dentry->d_inode;
650 struct ext2_dir_entry_2 * de;
654 if (it && it->it_disposition) {
656 invalidate_inode_pages(dir);
662 de = ext2_find_entry(dir, dentry, &page);
664 GOTO(out, rc = -ENOENT);
665 rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
666 dentry->d_name.name, dentry->d_name.len);
670 rc = ext2_delete_entry(de, page);
674 /* AED: not sure if needed - directory lock revocation should do it
675 * in the case where the client has cached it for non-intent ops.
677 invalidate_inode_pages(dir);
679 inode->i_ctime = dir->i_ctime;
681 ext2_dec_count(inode);
686 static int ll_unlink(struct inode *dir, struct dentry *dentry)
688 struct lookup_intent * it;
690 LL_GET_INTENT(dentry, it);
692 return ll_common_unlink(dir, dentry, it, S_IFREG);
695 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
697 struct inode * inode = dentry->d_inode;
698 struct lookup_intent *it;
702 LL_GET_INTENT(dentry, it);
705 if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
708 rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
711 ext2_dec_count(inode);
718 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
719 struct inode * new_dir, struct dentry * new_dentry)
721 struct lookup_intent *it;
722 struct inode * old_inode = old_dentry->d_inode;
723 struct inode * tgt_inode = new_dentry->d_inode;
724 struct page * dir_page = NULL;
725 struct ext2_dir_entry_2 * dir_de = NULL;
726 struct ext2_dir_entry_2 * old_de;
727 struct page * old_page;
730 LL_GET_INTENT(new_dentry, it);
732 if (it && it->it_disposition) {
734 tgt_inode->i_ctime = CURRENT_TIME;
735 tgt_inode->i_nlink--;
737 invalidate_inode_pages(old_dir);
738 invalidate_inode_pages(new_dir);
739 GOTO(out, err = it->it_status);
742 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
746 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
750 if (S_ISDIR(old_inode->i_mode)) {
752 dir_de = ext2_dotdot(old_inode, &dir_page);
758 struct page *new_page;
759 struct ext2_dir_entry_2 *new_de;
762 if (dir_de && !ext2_empty_dir (tgt_inode))
766 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
769 ext2_inc_count(old_inode);
770 ext2_set_link(new_dir, new_de, new_page, old_inode);
771 tgt_inode->i_ctime = CURRENT_TIME;
773 tgt_inode->i_nlink--;
774 ext2_dec_count(tgt_inode);
778 if (new_dir->i_nlink >= EXT2_LINK_MAX)
781 ext2_inc_count(old_inode);
782 err = ll_add_link(new_dentry, old_inode);
784 ext2_dec_count(old_inode);
788 ext2_inc_count(new_dir);
791 ext2_delete_entry (old_de, old_page);
792 ext2_dec_count(old_inode);
795 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
796 ext2_dec_count(old_dir);
803 page_cache_release(dir_page);
807 page_cache_release(old_page);
812 struct inode_operations ll_dir_inode_operations = {