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) {
293 CERROR("allocating fsdata\n");
296 d_add(dentry, inode);
298 if (it->it_status == 0) {
299 LL_SAVE_INTENT(dentry, it);
304 "D_IT dentry %p fsdata %p intent: %s status %d\n",
305 dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
309 if (it->it_op == IT_LOOKUP)
310 ll_intent_release(dentry, it);
315 ptlrpc_free_req(request);
317 #warning FIXME: must release lock here
321 static struct inode *ll_create_node(struct inode *dir, const char *name,
322 int namelen, const char *tgt, int tgtlen,
323 int mode, __u64 extra,
324 struct lookup_intent *it,
325 struct lov_stripe_md *lsm)
328 struct ptlrpc_request *request = NULL;
329 struct mds_body *body;
330 time_t time = CURRENT_TIME;
331 struct ll_sb_info *sbi = ll_i2sbi(dir);
332 struct ll_read_inode2_cookie lic;
333 struct lov_mds_md *lmm = NULL;
336 if (it && it->it_disposition) {
337 int rc = it->it_status;
339 CERROR("error creating MDS inode for %*s: rc = %d\n",
343 invalidate_inode_pages(dir);
344 request = it->it_data;
345 body = lustre_msg_buf(request->rq_repmsg, 1);
348 int gid = current->fsgid;
352 OBD_ALLOC(lmm, lsm->lsm_mds_easize);
354 RETURN(ERR_PTR(-ENOMEM));
355 lov_packmd(lmm, lsm);
360 if (dir->i_mode & S_ISGID) {
366 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
367 tgtlen, mode, current->fsuid, gid,
368 time, extra, lsm, &request);
373 body = lustre_msg_buf(request->rq_repmsg, 0);
378 LASSERT(body->ino != 0);
379 inode = iget4(dir->i_sb, body->ino, ll_find_inode, &lic);
381 int rc = PTR_ERR(inode);
382 CERROR("new_inode -fatal: rc %d\n", rc);
387 if (!list_empty(&inode->i_dentry)) {
388 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
389 body->ino, atomic_read(&inode->i_count),
393 inode = ERR_PTR(-EIO);
400 OBD_FREE(lmm, lsm->lsm_mds_easize);
401 ptlrpc_req_finished(request);
405 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
406 const char *name, int len)
408 struct ptlrpc_request *request = NULL;
409 struct ll_sb_info *sbi = ll_i2sbi(dir);
414 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
416 ptlrpc_req_finished(request);
421 int ll_mdc_link(struct dentry *src, struct inode *dir,
422 const char *name, int len)
424 struct ptlrpc_request *request = NULL;
426 struct ll_sb_info *sbi = ll_i2sbi(dir);
430 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
431 ptlrpc_req_finished(request);
436 int ll_mdc_rename(struct inode *src, struct inode *tgt,
437 struct dentry *old, struct dentry *new)
439 struct ptlrpc_request *request = NULL;
440 struct ll_sb_info *sbi = ll_i2sbi(src);
445 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
446 old->d_name.name, old->d_name.len,
447 new->d_name.name, new->d_name.len, &request);
448 ptlrpc_req_finished(request);
454 * By the time this is called, we already have created the directory cache
455 * entry for the new file, but it is so far negative - it has no inode.
456 * We defer creating the OBD object(s) until open, to keep the intent and
457 * non-intent code paths similar, and also because we do not have the MDS
458 * inode number before calling ll_create_node() (which is needed for LOV),
459 * so we would need to do yet another RPC to the MDS to store the LOV EA
462 * If the create succeeds, we fill in the inode information
463 * with d_instantiate().
465 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
467 struct lookup_intent *it;
472 CHECK_MOUNT_EPOCH(dir);
474 LL_GET_INTENT(dentry, it);
476 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
477 NULL, 0, mode, 0, it, NULL);
480 RETURN(PTR_ERR(inode));
482 if (it->it_disposition) {
483 struct ll_inode_info *lli = ll_i2info(inode);
484 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
485 sizeof(lli->lli_intent_lock_handle));
486 d_instantiate(dentry, inode);
488 /* no directory data updates when intents rule */
489 rc = ext2_add_nondir(dentry, inode);
495 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
498 struct lookup_intent *it;
502 LL_GET_INTENT(dentry, it);
504 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
505 NULL, 0, mode, rdev, it, NULL);
508 RETURN(PTR_ERR(inode));
510 /* no directory data updates when intents rule */
511 if (it && it->it_disposition)
512 d_instantiate(dentry, inode);
514 rc = ext2_add_nondir(dentry, inode);
519 static int ll_symlink(struct inode *dir, struct dentry *dentry,
522 struct lookup_intent *it;
523 unsigned l = strlen(symname);
525 struct ll_inode_info *lli;
529 CHECK_MOUNT_EPOCH(dir);
531 LL_GET_INTENT(dentry, it);
533 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
534 symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
536 RETURN(PTR_ERR(inode));
538 lli = ll_i2info(inode);
540 OBD_ALLOC(lli->lli_symlink_name, l + 1);
541 /* this _could_ be a non-fatal error, since the symlink is already
542 * stored on the MDS by this point, and we can re-get it in readlink.
544 if (!lli->lli_symlink_name)
547 memcpy(lli->lli_symlink_name, symname, l + 1);
550 /* no directory data updates when intents rule */
551 if (it && it->it_disposition)
552 d_instantiate(dentry, inode);
554 err = ext2_add_nondir(dentry, inode);
559 static int ll_link(struct dentry *old_dentry, struct inode * dir,
560 struct dentry *dentry)
562 struct lookup_intent *it;
563 struct inode *inode = old_dentry->d_inode;
566 LL_GET_INTENT(dentry, it);
568 if (it && it->it_disposition) {
570 RETURN(it->it_status);
571 inode->i_ctime = CURRENT_TIME;
572 ext2_inc_count(inode);
573 atomic_inc(&inode->i_count);
574 d_instantiate(dentry, inode);
575 invalidate_inode_pages(dir);
579 if (S_ISDIR(inode->i_mode))
582 if (inode->i_nlink >= EXT2_LINK_MAX)
585 rc = ll_mdc_link(old_dentry, dir,
586 dentry->d_name.name, dentry->d_name.len);
590 inode->i_ctime = CURRENT_TIME;
591 ext2_inc_count(inode);
592 atomic_inc(&inode->i_count);
594 return ext2_add_nondir(dentry, inode);
597 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
599 struct lookup_intent *it;
600 struct inode * inode;
604 LL_GET_INTENT(dentry, it);
606 if (dir->i_nlink >= EXT2_LINK_MAX)
613 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
614 NULL, 0, S_IFDIR | mode, 0, it, NULL);
615 err = PTR_ERR(inode);
619 ext2_inc_count(inode);
621 err = ext2_make_empty(inode, dir);
625 /* no directory data updates when intents rule */
626 if (!it || !it->it_disposition) {
627 err = ll_add_link(dentry, inode);
632 d_instantiate(dentry, inode);
638 ext2_dec_count(inode);
639 ext2_dec_count(inode);
648 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
649 struct lookup_intent *it, __u32 mode)
651 struct inode *inode = dentry->d_inode;
652 struct ext2_dir_entry_2 * de;
656 if (it && it->it_disposition) {
658 invalidate_inode_pages(dir);
664 de = ext2_find_entry(dir, dentry, &page);
666 GOTO(out, rc = -ENOENT);
667 rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
668 dentry->d_name.name, dentry->d_name.len);
672 rc = ext2_delete_entry(de, page);
676 /* AED: not sure if needed - directory lock revocation should do it
677 * in the case where the client has cached it for non-intent ops.
679 invalidate_inode_pages(dir);
681 inode->i_ctime = dir->i_ctime;
683 ext2_dec_count(inode);
688 static int ll_unlink(struct inode *dir, struct dentry *dentry)
690 struct lookup_intent * it;
692 LL_GET_INTENT(dentry, it);
694 return ll_common_unlink(dir, dentry, it, S_IFREG);
697 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
699 struct inode * inode = dentry->d_inode;
700 struct lookup_intent *it;
704 LL_GET_INTENT(dentry, it);
707 if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
710 rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
713 ext2_dec_count(inode);
720 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
721 struct inode * new_dir, struct dentry * new_dentry)
723 struct lookup_intent *it;
724 struct inode * old_inode = old_dentry->d_inode;
725 struct inode * tgt_inode = new_dentry->d_inode;
726 struct page * dir_page = NULL;
727 struct ext2_dir_entry_2 * dir_de = NULL;
728 struct ext2_dir_entry_2 * old_de;
729 struct page * old_page;
732 LL_GET_INTENT(new_dentry, it);
734 if (it && it->it_disposition) {
736 tgt_inode->i_ctime = CURRENT_TIME;
737 tgt_inode->i_nlink--;
739 invalidate_inode_pages(old_dir);
740 invalidate_inode_pages(new_dir);
741 GOTO(out, err = it->it_status);
744 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
748 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
752 if (S_ISDIR(old_inode->i_mode)) {
754 dir_de = ext2_dotdot(old_inode, &dir_page);
760 struct page *new_page;
761 struct ext2_dir_entry_2 *new_de;
764 if (dir_de && !ext2_empty_dir (tgt_inode))
768 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
771 ext2_inc_count(old_inode);
772 ext2_set_link(new_dir, new_de, new_page, old_inode);
773 tgt_inode->i_ctime = CURRENT_TIME;
775 tgt_inode->i_nlink--;
776 ext2_dec_count(tgt_inode);
780 if (new_dir->i_nlink >= EXT2_LINK_MAX)
783 ext2_inc_count(old_inode);
784 err = ll_add_link(new_dentry, old_inode);
786 ext2_dec_count(old_inode);
790 ext2_inc_count(new_dir);
793 ext2_delete_entry (old_de, old_page);
794 ext2_dec_count(old_inode);
797 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
798 ext2_dec_count(old_dir);
805 page_cache_release(dir_page);
809 page_cache_release(old_page);
814 struct inode_operations ll_dir_inode_operations = {