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_inode_md *md = opaque;
91 if (inode->i_generation != md->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))
112 else if (it->it_op & IT_SYMLINK) {
115 tgtlen = strlen(tgt);
117 } else if (it->it_op & IT_LOOKUP)
124 err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode, dir,
125 dentry, lockh, 0, tgt, tgtlen, dir, sizeof(*dir));
130 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
134 ldlm_lock_decref(lockh, mode);
139 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
140 struct lookup_intent *it)
142 struct ptlrpc_request *request = NULL;
143 struct inode * inode = NULL;
144 struct ll_sb_info *sbi = ll_i2sbi(dir);
145 struct ll_inode_md md;
146 struct lustre_handle lockh;
147 struct lookup_intent lookup_it = { IT_LOOKUP };
148 int err, offset, mode;
158 CDEBUG(D_INFO, "name: %*s, intent op: %d\n", dentry->d_name.len,
159 dentry->d_name.name, it->it_op);
161 if (dentry->d_name.len > EXT2_NAME_LEN)
162 RETURN(ERR_PTR(-ENAMETOOLONG));
164 err = ll_lock(dir, dentry, it, &lockh);
166 RETURN(ERR_PTR(err));
167 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
169 request = (struct ptlrpc_request *)it->it_data;
170 if (it->it_disposition) {
172 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
173 /* For create ops, we want the lookup to be negative */
175 GOTO(negative, NULL);
176 } else if (it->it_op & (IT_GETATTR | IT_UNLINK |
177 IT_RMDIR | IT_SETATTR | IT_LOOKUP)) {
178 /* For remove/check, we want the lookup to succeed */
182 } else if (it->it_op & IT_RENAME) {
187 it->it_data = dentry;
188 } else if (it->it_op == IT_OPEN) {
190 if (it->it_status && it->it_status != -EEXIST)
192 } else if (it->it_op == IT_RENAME2) {
193 struct mds_body *body =
194 lustre_msg_buf(request->rq_repmsg, offset);
196 if (body->valid == 0)
201 /* Do a getattr now that we have the lock */
202 if ((it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) &&
204 /* the unlink/rmdir succeeded, there's nothing to
207 md.body = lustre_msg_buf(request->rq_repmsg, offset);
208 ino = md.body->fid1.id;
209 mode = md.body->mode;
210 ptlrpc_free_req(request);
212 err = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
213 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
215 CERROR("failure %d inode %Ld\n", err, (long long)ino);
216 ptlrpc_free_req(request);
217 #warning FIXME: must release lock here
218 RETURN(ERR_PTR(-abs(err)));
222 struct ll_inode_info *lli = ll_i2info(dir);
225 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
228 ino = ll_inode_by_name(dir, dentry, &type);
229 #warning FIXME: handle negative inode case (see old ll_lookup)
231 err = mdc_getattr(&sbi->ll_mdc_conn, ino, type,
232 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
234 CERROR("failure %d inode %Ld\n", err, (long long)ino);
235 ptlrpc_free_req(request);
236 #warning FIXME: must release lock here
237 RETURN(ERR_PTR(-abs(err)));
242 md.body = lustre_msg_buf(request->rq_repmsg, offset);
243 if (S_ISREG(md.body->mode)) {
244 if (request->rq_repmsg->bufcount < offset + 1)
246 md.md = lustre_msg_buf(request->rq_repmsg, offset + 1);
250 /* No rpc's happen during iget4, -ENOMEM's are possible */
251 inode = iget4(dir->i_sb, ino, ll_find_inode, &md);
253 LASSERT(!IS_ERR(inode));
255 ptlrpc_free_req(request);
256 ll_intent_release(dentry);
257 RETURN(ERR_PTR(-ENOMEM));
262 ptlrpc_free_req(request);
264 dentry->d_op = &ll_d_ops;
265 d_add(dentry, inode);
266 if (it->it_op == IT_LOOKUP)
267 ll_intent_release(dentry);
272 static struct inode *ll_create_node(struct inode *dir, const char *name,
273 int namelen, const char *tgt, int tgtlen,
274 int mode, __u64 extra,
275 struct lookup_intent *it,
276 struct lov_stripe_md *smd)
279 struct ptlrpc_request *request = NULL;
280 struct mds_body *body;
282 time_t time = CURRENT_TIME;
283 struct ll_sb_info *sbi = ll_i2sbi(dir);
284 int gid = current->fsgid;
285 struct ll_inode_md md;
286 struct lov_mds_md *mds_md = NULL;
291 if (dir->i_mode & S_ISGID) {
297 if (!it || !it->it_disposition) {
298 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
299 tgtlen, mode, current->fsuid,
300 gid, time, extra, smd, &request);
305 body = lustre_msg_buf(request->rq_repmsg, 0);
307 mds_md_size = sizeof (struct lov_mds_md) +
308 smd->lmd_stripe_count * sizeof(struct lov_object_id);
309 OBD_ALLOC(mds_md, mds_md_size);
310 lov_packmd(mds_md, smd);
316 request = it->it_data;
317 body = lustre_msg_buf(request->rq_repmsg, 1);
321 body->valid = OBD_MD_FLNOTOBD;
324 body->atime = body->ctime = body->mtime = time;
325 body->uid = current->fsuid;
331 inode = iget4(dir->i_sb, body->ino, ll_find_inode, &md);
334 CERROR("new_inode -fatal: rc %d\n", rc);
339 if (!list_empty(&inode->i_dentry)) {
340 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
341 body->ino, atomic_read(&inode->i_count),
345 inode = ERR_PTR(-EIO);
352 OBD_FREE(mds_md, mds_md_size);
353 ptlrpc_free_req(request);
357 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
358 const char *name, int len)
360 struct ptlrpc_request *request = NULL;
361 struct ll_sb_info *sbi = ll_i2sbi(dir);
366 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
368 ptlrpc_free_req(request);
373 int ll_mdc_link(struct dentry *src, struct inode *dir,
374 const char *name, int len)
376 struct ptlrpc_request *request = NULL;
378 struct ll_sb_info *sbi = ll_i2sbi(dir);
382 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name,
384 ptlrpc_free_req(request);
389 int ll_mdc_rename(struct inode *src, struct inode *tgt,
390 struct dentry *old, struct dentry *new)
392 struct ptlrpc_request *request = NULL;
393 struct ll_sb_info *sbi = ll_i2sbi(src);
398 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
399 old->d_name.name, old->d_name.len,
400 new->d_name.name, new->d_name.len, &request);
401 ptlrpc_free_req(request);
407 * By the time this is called, we already have created
408 * the directory cache entry for the new file, but it
409 * is so far negative - it has no inode.
411 * If the create succeeds, we fill in the inode information
412 * with d_instantiate().
415 static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
420 struct lov_stripe_md *smd = NULL;
421 struct ll_inode_info *lli = NULL;
424 if (dentry->d_it->it_disposition == 0) {
425 memset(&oa, 0, sizeof(oa));
426 oa.o_valid = OBD_MD_FLMODE;
427 oa.o_mode = S_IFREG | 0600;
428 rc = obd_create(ll_i2obdconn(dir), &oa, &smd);
429 CDEBUG(D_DENTRY, "name %s mode %o o_id %lld: rc = %d\n",
430 dentry->d_name.name, mode, (long long)oa.o_id, rc);
435 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
436 NULL, 0, mode, 0, dentry->d_it, smd);
440 CERROR("error creating MDS object for id %Ld: rc = %d\n",
441 (unsigned long long)oa.o_id, rc);
442 GOTO(out_destroy, rc);
445 if (dentry->d_it->it_disposition) {
446 lli = ll_i2info(inode);
447 memcpy(&lli->lli_intent_lock_handle,
448 dentry->d_it->it_lock_handle,
449 sizeof(struct lustre_handle));
450 d_instantiate(dentry, inode);
452 /* no directory data updates when intents rule */
453 rc = ext2_add_nondir(dentry, inode);
460 oa.o_easize = smd->lmd_easize;
461 oa.o_valid |= OBD_MD_FLEASIZE;
462 err = obd_destroy(ll_i2obdconn(dir), &oa, smd);
464 CERROR("error destroying objid %Ld on error: err %d\n",
465 (unsigned long long)oa.o_id, err);
471 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
477 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
478 NULL, 0, mode, rdev, dentry->d_it, NULL);
481 RETURN(PTR_ERR(inode));
483 /* no directory data updates when intents rule */
484 if (dentry->d_it && dentry->d_it->it_disposition)
485 d_instantiate(dentry, inode);
487 err = ext2_add_nondir(dentry, inode);
492 static int ll_symlink(struct inode *dir, struct dentry *dentry,
495 unsigned l = strlen(symname);
497 struct ll_inode_info *lli;
501 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
502 symname, l, S_IFLNK | S_IRWXUGO, 0,
505 RETURN(PTR_ERR(inode));
507 lli = ll_i2info(inode);
509 OBD_ALLOC(lli->lli_symlink_name, l + 1);
510 /* this _could_ be a non-fatal error, since the symlink is already
511 * stored on the MDS by this point, and we can re-get it in readlink.
513 if (!lli->lli_symlink_name)
516 memcpy(lli->lli_symlink_name, symname, l + 1);
519 /* no directory data updates when intents rule */
520 if (dentry->d_it && dentry->d_it->it_disposition)
521 d_instantiate(dentry, inode);
523 err = ext2_add_nondir(dentry, inode);
528 static int ll_link(struct dentry *old_dentry, struct inode * dir,
529 struct dentry *dentry)
532 struct inode *inode = old_dentry->d_inode;
534 #warning FIXME: still needs intent support
535 if (S_ISDIR(inode->i_mode))
538 if (inode->i_nlink >= EXT2_LINK_MAX)
541 err = ll_mdc_link(old_dentry, dir,
542 dentry->d_name.name, dentry->d_name.len);
546 inode->i_ctime = CURRENT_TIME;
547 ext2_inc_count(inode);
548 atomic_inc(&inode->i_count);
550 return ext2_add_nondir(dentry, inode);
553 static int ll_mkdir(struct inode * dir, struct dentry * dentry, int mode)
555 struct inode * inode;
559 if (dir->i_nlink >= EXT2_LINK_MAX)
564 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
565 NULL, 0, S_IFDIR | mode, 0, dentry->d_it, NULL);
566 err = PTR_ERR(inode);
570 ext2_inc_count(inode);
572 err = ext2_make_empty(inode, dir);
576 /* no directory data updates when intents rule */
577 if (dentry->d_it->it_disposition == 0) {
578 err = ll_add_link(dentry, inode);
583 d_instantiate(dentry, inode);
589 ext2_dec_count(inode);
590 ext2_dec_count(inode);
599 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
602 struct inode * inode = dentry->d_inode;
603 struct ext2_dir_entry_2 * de;
607 if (dentry->d_it && dentry->d_it->it_disposition) {
608 err = dentry->d_it->it_status;
612 de = ext2_find_entry(dir, dentry, &page);
616 err = ll_mdc_unlink(dir, dentry->d_inode, mode,
617 dentry->d_name.name, dentry->d_name.len);
621 err = ext2_delete_entry(de, page);
625 inode->i_ctime = dir->i_ctime;
627 ext2_dec_count(inode);
631 static int ll_unlink(struct inode *dir, struct dentry *dentry)
633 return ll_common_unlink(dir, dentry, S_IFREG);
636 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
638 struct inode * inode = dentry->d_inode;
642 if (!dentry->d_it || dentry->d_it->it_disposition == 0) {
643 if (!ext2_empty_dir(inode))
646 err = ll_common_unlink(dir, dentry, S_IFDIR);
648 err = dentry->d_it->it_status;
652 ext2_dec_count(inode);
657 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
658 struct inode * new_dir, struct dentry * new_dentry)
660 struct inode * old_inode = old_dentry->d_inode;
661 struct inode * new_inode = new_dentry->d_inode;
662 struct page * dir_page = NULL;
663 struct ext2_dir_entry_2 * dir_de = NULL;
664 struct ext2_dir_entry_2 * old_de;
665 struct page * old_page;
668 if (new_dentry->d_it && new_dentry->d_it->it_disposition) {
670 new_inode->i_ctime = CURRENT_TIME;
671 new_inode->i_nlink--;
673 GOTO(out, err = new_dentry->d_it->it_status);
676 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
680 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
684 if (S_ISDIR(old_inode->i_mode)) {
686 dir_de = ext2_dotdot(old_inode, &dir_page);
692 struct page *new_page;
693 struct ext2_dir_entry_2 *new_de;
696 if (dir_de && !ext2_empty_dir (new_inode))
700 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
703 ext2_inc_count(old_inode);
704 ext2_set_link(new_dir, new_de, new_page, old_inode);
705 new_inode->i_ctime = CURRENT_TIME;
707 new_inode->i_nlink--;
708 ext2_dec_count(new_inode);
712 if (new_dir->i_nlink >= EXT2_LINK_MAX)
715 ext2_inc_count(old_inode);
716 err = ll_add_link(new_dentry, old_inode);
718 ext2_dec_count(old_inode);
722 ext2_inc_count(new_dir);
725 ext2_delete_entry (old_de, old_page);
726 ext2_dec_count(old_inode);
729 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
730 ext2_dec_count(old_dir);
737 page_cache_release(dir_page);
741 page_cache_release(old_page);
746 struct inode_operations ll_dir_inode_operations = {