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/locks.h>
32 #include <linux/quotaops.h>
34 #define DEBUG_SUBSYSTEM S_LLITE
36 #include <linux/obd_support.h>
37 #include <linux/lustre_lite.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/obd_lov.h>
41 extern struct address_space_operations ll_aops;
44 extern void ll_change_inode(struct inode *inode);
45 extern int ll_setattr(struct dentry *de, struct iattr *attr);
48 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
49 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
50 int ext2_make_empty(struct inode *inode, struct inode *parent);
51 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
52 struct dentry *dentry, struct page ** res_page);
53 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
54 int ext2_empty_dir (struct inode * inode);
55 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
56 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
57 struct page *page, struct inode *inode);
60 * Couple of helper functions - make the code slightly cleaner.
62 static inline void ext2_inc_count(struct inode *inode)
67 /* postpone the disk update until the inode really goes away */
68 static inline void ext2_dec_count(struct inode *inode)
73 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
76 err = ll_add_link(dentry, inode);
78 d_instantiate(dentry, inode);
81 ext2_dec_count(inode);
87 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
89 struct ll_read_inode2_cookie *lic = opaque;
91 if (inode->i_generation != lic->lic_body->generation)
97 extern struct dentry_operations ll_d_ops;
99 int ll_lock(struct inode *dir, struct dentry *dentry,
100 struct lookup_intent *it, struct lustre_handle *lockh)
102 struct ll_sb_info *sbi = ll_i2sbi(dir);
107 if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD)))
109 else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
110 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
113 else if (it->it_op & IT_SYMLINK) {
116 tgtlen = strlen(tgt);
118 } else if (it->it_op & IT_LOOKUP)
125 err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode,
126 dir, dentry, lockh, tgt, tgtlen, dir, sizeof(*dir));
131 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
135 ldlm_lock_decref(lockh, mode);
140 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
141 struct lookup_intent *it)
143 struct ptlrpc_request *request = NULL;
144 struct inode * inode = NULL;
145 struct ll_sb_info *sbi = ll_i2sbi(dir);
146 struct ll_read_inode2_cookie lic;
147 struct lustre_handle lockh;
148 struct lookup_intent lookup_it = { IT_LOOKUP };
159 CDEBUG(D_INFO, "name: %*s, intent op: %d\n", dentry->d_name.len,
160 dentry->d_name.name, it->it_op);
162 if (dentry->d_name.len > EXT2_NAME_LEN)
163 RETURN(ERR_PTR(-ENAMETOOLONG));
165 err = ll_lock(dir, dentry, it, &lockh);
167 RETURN(ERR_PTR(err));
168 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
170 request = (struct ptlrpc_request *)it->it_data;
171 if (it->it_disposition) {
172 int mode, symlen = 0;
176 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
177 /* For create ops, we want the lookup to be negative */
179 GOTO(negative, NULL);
180 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP)) {
181 /* For check ops, we want the lookup to succeed */
185 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
186 /* For rename, we want the lookup to succeed */
191 it->it_data = dentry;
192 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
193 /* For remove ops, we want the lookup to succeed */
198 } else if (it->it_op == IT_OPEN) {
200 if (it->it_status && it->it_status != -EEXIST)
202 } else if (it->it_op & (IT_RENAME2|IT_LINK2)) {
203 struct mds_body *body =
204 lustre_msg_buf(request->rq_repmsg, offset);
206 /* For rename2, this means the lookup is negative */
208 if (body->valid == 0)
210 goto iget; /* XXX not sure about this */
213 /* Do a getattr now that we have the lock */
214 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
215 ino = lic.lic_body->fid1.id;
216 mode = lic.lic_body->mode;
217 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
218 if (it->it_op == IT_READLINK) {
219 valid |= OBD_MD_LINKNAME;
220 symlen = lic.lic_body->size;
222 ptlrpc_free_req(request);
224 err = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
225 valid, symlen, &request);
227 CERROR("failure %d inode %Ld\n", err, (long long)ino);
228 ptlrpc_free_req(request);
229 #warning FIXME: must release lock here
230 RETURN(ERR_PTR(-abs(err)));
234 struct ll_inode_info *lli = ll_i2info(dir);
237 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
240 ino = ll_inode_by_name(dir, dentry, &type);
241 #warning FIXME: handle negative inode case (see old ll_lookup)
243 err = mdc_getattr(&sbi->ll_mdc_conn, ino, type,
244 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
246 CERROR("failure %d inode %Ld\n", err, (long long)ino);
247 ptlrpc_free_req(request);
248 #warning FIXME: must release lock here
249 RETURN(ERR_PTR(-abs(err)));
254 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
255 if (S_ISREG(lic.lic_body->mode) &&
256 lic.lic_body->valid & OBD_MD_FLEASIZE) {
257 LASSERT(request->rq_repmsg->bufcount > offset);
258 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
262 /* No rpc's happen during iget4, -ENOMEM's are possible */
263 inode = iget4(dir->i_sb, ino, ll_find_inode, &lic);
265 LASSERT(!IS_ERR(inode));
267 ptlrpc_free_req(request);
268 ll_intent_release(dentry);
269 RETURN(ERR_PTR(-ENOMEM));
274 ptlrpc_req_finished(request);
276 dentry->d_op = &ll_d_ops;
277 d_add(dentry, inode);
278 if (it->it_op == IT_LOOKUP)
279 ll_intent_release(dentry);
284 static struct inode *ll_create_node(struct inode *dir, const char *name,
285 int namelen, const char *tgt, int tgtlen,
286 int mode, __u64 extra,
287 struct lookup_intent *it,
288 struct lov_stripe_md *lsm)
291 struct ptlrpc_request *request = NULL;
292 struct mds_body *body;
294 time_t time = CURRENT_TIME;
295 struct ll_sb_info *sbi = ll_i2sbi(dir);
296 int gid = current->fsgid;
297 struct ll_read_inode2_cookie lic;
298 struct lov_mds_md *lmm = NULL;
303 if (dir->i_mode & S_ISGID) {
309 if (!it || !it->it_disposition) {
310 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
311 tgtlen, mode, current->fsuid,
312 gid, time, extra, lsm, &request);
317 body = lustre_msg_buf(request->rq_repmsg, 0);
319 mds_md_size = ll_mds_easize(dir->i_sb);
320 OBD_ALLOC(lmm, mds_md_size);
321 lov_packmd(lmm, lsm);
327 invalidate_inode_pages(dir);
328 request = it->it_data;
329 body = lustre_msg_buf(request->rq_repmsg, 1);
333 body->valid = OBD_MD_FLNOTOBD;
336 body->atime = body->ctime = body->mtime = time;
337 body->uid = current->fsuid;
343 inode = iget4(dir->i_sb, body->ino, ll_find_inode, &lic);
346 CERROR("new_inode -fatal: rc %d\n", rc);
351 if (!list_empty(&inode->i_dentry)) {
352 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
353 body->ino, atomic_read(&inode->i_count),
357 inode = ERR_PTR(-EIO);
364 OBD_FREE(lmm, mds_md_size);
365 ptlrpc_free_req(request);
369 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
370 const char *name, int len)
372 struct ptlrpc_request *request = NULL;
373 struct ll_sb_info *sbi = ll_i2sbi(dir);
378 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
380 ptlrpc_free_req(request);
385 int ll_mdc_link(struct dentry *src, struct inode *dir,
386 const char *name, int len)
388 struct ptlrpc_request *request = NULL;
390 struct ll_sb_info *sbi = ll_i2sbi(dir);
394 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name,
396 ptlrpc_free_req(request);
401 int ll_mdc_rename(struct inode *src, struct inode *tgt,
402 struct dentry *old, struct dentry *new)
404 struct ptlrpc_request *request = NULL;
405 struct ll_sb_info *sbi = ll_i2sbi(src);
410 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
411 old->d_name.name, old->d_name.len,
412 new->d_name.name, new->d_name.len, &request);
413 ptlrpc_free_req(request);
419 * By the time this is called, we already have created
420 * the directory cache entry for the new file, but it
421 * is so far negative - it has no inode.
423 * If the create succeeds, we fill in the inode information
424 * with d_instantiate().
427 static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
432 struct lov_stripe_md *lsm = NULL;
433 struct ll_inode_info *lli = NULL;
436 if (dentry->d_it->it_disposition == 0) {
437 memset(&oa, 0, sizeof(oa));
438 oa.o_valid = OBD_MD_FLMODE;
439 oa.o_mode = S_IFREG | 0600;
440 rc = obd_create(ll_i2obdconn(dir), &oa, &lsm);
441 CDEBUG(D_DENTRY, "name %s mode %o o_id %lld: rc = %d\n",
442 dentry->d_name.name, mode, (long long)oa.o_id, rc);
447 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
448 NULL, 0, mode, 0, dentry->d_it, lsm);
452 CERROR("error creating MDS object for id %Ld: rc = %d\n",
453 (unsigned long long)oa.o_id, rc);
454 GOTO(out_destroy, rc);
457 if (dentry->d_it->it_disposition) {
458 lli = ll_i2info(inode);
459 memcpy(&lli->lli_intent_lock_handle,
460 dentry->d_it->it_lock_handle,
461 sizeof(struct lustre_handle));
462 d_instantiate(dentry, inode);
464 /* no directory data updates when intents rule */
465 rc = ext2_add_nondir(dentry, inode);
472 oa.o_easize = ll_mds_easize(inode->i_sb);
473 oa.o_valid |= OBD_MD_FLEASIZE;
474 err = obd_destroy(ll_i2obdconn(dir), &oa, lsm);
476 CERROR("error destroying objid %Ld on error: err %d\n",
477 (unsigned long long)oa.o_id, err);
483 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
489 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
490 NULL, 0, mode, rdev, dentry->d_it, NULL);
493 RETURN(PTR_ERR(inode));
495 /* no directory data updates when intents rule */
496 if (dentry->d_it && dentry->d_it->it_disposition)
497 d_instantiate(dentry, inode);
499 err = ext2_add_nondir(dentry, inode);
504 static int ll_symlink(struct inode *dir, struct dentry *dentry,
507 unsigned l = strlen(symname);
509 struct ll_inode_info *lli;
513 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
514 symname, l, S_IFLNK | S_IRWXUGO, 0,
517 RETURN(PTR_ERR(inode));
519 lli = ll_i2info(inode);
521 OBD_ALLOC(lli->lli_symlink_name, l + 1);
522 /* this _could_ be a non-fatal error, since the symlink is already
523 * stored on the MDS by this point, and we can re-get it in readlink.
525 if (!lli->lli_symlink_name)
528 memcpy(lli->lli_symlink_name, symname, l + 1);
531 /* no directory data updates when intents rule */
532 if (dentry->d_it && dentry->d_it->it_disposition)
533 d_instantiate(dentry, inode);
535 err = ext2_add_nondir(dentry, inode);
540 static int ll_link(struct dentry *old_dentry, struct inode * dir,
541 struct dentry *dentry)
544 struct inode *inode = old_dentry->d_inode;
546 if (dentry->d_it && dentry->d_it->it_disposition) {
547 int err = dentry->d_it->it_status;
550 inode->i_ctime = CURRENT_TIME;
551 ext2_inc_count(inode);
552 atomic_inc(&inode->i_count);
553 d_instantiate(dentry, inode);
554 invalidate_inode_pages(dir);
558 if (S_ISDIR(inode->i_mode))
561 if (inode->i_nlink >= EXT2_LINK_MAX)
564 err = ll_mdc_link(old_dentry, dir,
565 dentry->d_name.name, dentry->d_name.len);
569 inode->i_ctime = CURRENT_TIME;
570 ext2_inc_count(inode);
571 atomic_inc(&inode->i_count);
573 return ext2_add_nondir(dentry, inode);
576 static int ll_mkdir(struct inode * dir, struct dentry * dentry, int mode)
578 struct inode * inode;
582 if (dir->i_nlink >= EXT2_LINK_MAX)
587 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
588 NULL, 0, S_IFDIR | mode, 0, dentry->d_it, NULL);
589 err = PTR_ERR(inode);
593 ext2_inc_count(inode);
595 err = ext2_make_empty(inode, dir);
599 /* no directory data updates when intents rule */
600 if (dentry->d_it->it_disposition == 0) {
601 err = ll_add_link(dentry, inode);
606 d_instantiate(dentry, inode);
612 ext2_dec_count(inode);
613 ext2_dec_count(inode);
622 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
625 struct inode * inode = dentry->d_inode;
627 struct ext2_dir_entry_2 * de;
632 if (dentry->d_it && dentry->d_it->it_disposition) {
633 err = dentry->d_it->it_status;
634 invalidate_inode_pages(dir);
639 de = ext2_find_entry(dir, dentry, &page);
643 err = ll_mdc_unlink(dir, dentry->d_inode, mode,
644 dentry->d_name.name, dentry->d_name.len);
649 err = ext2_delete_entry(de, page);
653 invalidate_inode_pages(dir);
655 inode->i_ctime = dir->i_ctime;
657 ext2_dec_count(inode);
661 static int ll_unlink(struct inode *dir, struct dentry *dentry)
663 return ll_common_unlink(dir, dentry, S_IFREG);
666 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
668 struct inode * inode = dentry->d_inode;
672 if (!dentry->d_it || dentry->d_it->it_disposition == 0) {
673 if (!ext2_empty_dir(inode))
675 err = ll_common_unlink(dir, dentry, S_IFDIR);
677 err = dentry->d_it->it_status;
681 ext2_dec_count(inode);
686 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
687 struct inode * new_dir, struct dentry * new_dentry)
689 struct inode * old_inode = old_dentry->d_inode;
690 struct inode * tgt_inode = new_dentry->d_inode;
691 struct page * dir_page = NULL;
692 struct ext2_dir_entry_2 * dir_de = NULL;
693 struct ext2_dir_entry_2 * old_de;
694 struct page * old_page;
697 if (new_dentry->d_it && new_dentry->d_it->it_disposition) {
699 tgt_inode->i_ctime = CURRENT_TIME;
700 tgt_inode->i_nlink--;
702 invalidate_inode_pages(old_dir);
703 invalidate_inode_pages(new_dir);
704 GOTO(out, err = new_dentry->d_it->it_status);
707 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
711 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
715 if (S_ISDIR(old_inode->i_mode)) {
717 dir_de = ext2_dotdot(old_inode, &dir_page);
723 struct page *new_page;
724 struct ext2_dir_entry_2 *new_de;
727 if (dir_de && !ext2_empty_dir (tgt_inode))
731 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
734 ext2_inc_count(old_inode);
735 ext2_set_link(new_dir, new_de, new_page, old_inode);
736 tgt_inode->i_ctime = CURRENT_TIME;
738 tgt_inode->i_nlink--;
739 ext2_dec_count(tgt_inode);
743 if (new_dir->i_nlink >= EXT2_LINK_MAX)
746 ext2_inc_count(old_inode);
747 err = ll_add_link(new_dentry, old_inode);
749 ext2_dec_count(old_inode);
753 ext2_inc_count(new_dir);
756 ext2_delete_entry (old_de, old_page);
757 ext2_dec_count(old_inode);
760 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
761 ext2_dec_count(old_dir);
768 page_cache_release(dir_page);
772 page_cache_release(old_page);
777 struct inode_operations ll_dir_inode_operations = {