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 extern struct address_space_operations ll_aops;
42 extern void ll_change_inode(struct inode *inode);
43 extern int ll_setattr(struct dentry *de, struct iattr *attr);
46 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
47 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
48 int ext2_make_empty(struct inode *inode, struct inode *parent);
49 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
50 struct dentry *dentry, struct page ** res_page);
51 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
52 int ext2_empty_dir (struct inode * inode);
53 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
54 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
55 struct page *page, struct inode *inode);
58 * Couple of helper functions - make the code slightly cleaner.
60 static inline void ext2_inc_count(struct inode *inode)
65 /* postpone the disk update until the inode really goes away */
66 static inline void ext2_dec_count(struct inode *inode)
71 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
74 err = ll_add_link(dentry, inode);
76 d_instantiate(dentry, inode);
79 ext2_dec_count(inode);
85 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
87 struct mds_body *body = (struct mds_body *)opaque;
89 if (inode->i_generation != body->generation)
95 extern struct dentry_operations ll_d_ops;
97 int ll_lock(struct inode *dir, struct dentry *dentry,
98 struct lookup_intent *it, struct lustre_handle *lockh)
100 struct ll_sb_info *sbi = ll_i2sbi(dir);
103 if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_SETATTR |
106 else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
107 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK))
109 else if (it->it_op & IT_LOOKUP)
116 #warning FIXME: add symlink tgt to intent and as a parameter here
117 err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode, dir,
118 dentry, lockh, 0, NULL, 0, dir, sizeof(*dir));
123 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
127 ldlm_lock_decref(lockh, mode);
132 static struct dentry *ll_lookup2(struct inode * dir, struct dentry *dentry,
133 struct lookup_intent *it)
135 struct ptlrpc_request *request = NULL;
136 struct inode * inode = NULL;
137 struct ll_sb_info *sbi = ll_i2sbi(dir);
138 struct ll_inode_md md;
139 struct lustre_handle lockh;
141 struct lookup_intent lookup_it = { IT_LOOKUP };
151 CDEBUG(D_INFO, "name: %*s, intent op: %d\n", dentry->d_name.len,
152 dentry->d_name.name, it->it_op);
154 if (dentry->d_name.len > EXT2_NAME_LEN)
155 RETURN(ERR_PTR(-ENAMETOOLONG));
157 if (it->it_op == IT_RENAME2) {
158 /* Set below to be the old dentry from the IT_RENAME intent */
159 inode = ((struct dentry *)(it->it_data))->d_inode;
163 err = ll_lock(dir, dentry, it, &lockh);
165 RETURN(ERR_PTR(err));
166 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
168 request = (struct ptlrpc_request *)it->it_data;
169 if (it->it_disposition) {
171 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
172 /* For create ops, we want the lookup to be negative */
174 GOTO(negative, NULL);
175 } else if (it->it_op & (IT_GETATTR | IT_UNLINK |
176 IT_RMDIR | IT_SETATTR | IT_LOOKUP)) {
177 /* For remove/check, we want the lookup to succeed */
181 } else if (it->it_op & IT_RENAME) {
186 LASSERT(dentry->d_inode);
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 /* FIXME: make old_de negative, and drop
194 * new_de->d_inode if needed.
200 struct ll_inode_info *lli = ll_i2info(dir);
203 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
206 ino = ll_inode_by_name(dir, dentry, &type);
207 #warning FIXME: handle negative inode case (see old ll_lookup)
209 err = mdc_getattr(&sbi->ll_mdc_conn, ino, type,
210 OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
212 CERROR("failure %d inode %Ld\n", err, (long long)ino);
213 ptlrpc_free_req(request);
214 #warning FIXME: must release lock here
215 RETURN(ERR_PTR(-abs(err)));
219 md.body = lustre_msg_buf(request->rq_repmsg, offset);
220 if (S_ISREG(md.body->mode)) {
221 if (request->rq_repmsg->bufcount < offset + 1)
223 md.md = lustre_msg_buf(request->rq_repmsg, offset + 1);
227 /* No rpc's happen during iget4, -ENOMEM's are possible */
228 inode = iget4(dir->i_sb, ino, ll_find_inode, &md);
231 LASSERT(!IS_ERR(inode));
233 ptlrpc_free_req(request);
234 ll_intent_release(dentry);
235 RETURN(ERR_PTR(-ENOMEM));
239 ptlrpc_free_req(request);
241 dentry->d_op = &ll_d_ops;
242 d_add(dentry, inode);
243 if (it->it_op == IT_LOOKUP)
244 ll_intent_release(dentry);
249 static struct inode *ll_create_node(struct inode *dir, const char *name,
250 int namelen, const char *tgt, int tgtlen,
251 int mode, __u64 extra,
252 struct lookup_intent *it,
253 struct lov_stripe_md *smd)
256 struct ptlrpc_request *request = NULL;
257 struct mds_body *body;
259 time_t time = CURRENT_TIME;
260 struct ll_sb_info *sbi = ll_i2sbi(dir);
261 int gid = current->fsgid;
262 struct ll_inode_md md;
266 if (dir->i_mode & S_ISGID) {
272 if (!it || !it->it_disposition) {
273 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
274 tgtlen, mode, current->fsuid,
275 gid, time, extra, smd, &request);
280 body = lustre_msg_buf(request->rq_repmsg, 0);
283 request = it->it_data;
284 body = lustre_msg_buf(request->rq_repmsg, 1);
288 body->valid = OBD_MD_FLNOTOBD;
291 body->atime = body->ctime = body->mtime = time;
292 body->uid = current->fsuid;
298 inode = iget4(dir->i_sb, body->ino, ll_find_inode, &md);
301 CERROR("new_inode -fatal: rc %d\n", rc);
306 if (!list_empty(&inode->i_dentry)) {
307 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
308 body->ino, atomic_read(&inode->i_count),
312 inode = ERR_PTR(-EIO);
318 ptlrpc_free_req(request);
322 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
323 const char *name, int len)
325 struct ptlrpc_request *request = NULL;
326 struct ll_sb_info *sbi = ll_i2sbi(dir);
331 err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
333 ptlrpc_free_req(request);
338 int ll_mdc_link(struct dentry *src, struct inode *dir,
339 const char *name, int len)
341 struct ptlrpc_request *request = NULL;
343 struct ll_sb_info *sbi = ll_i2sbi(dir);
347 err = mdc_link(&sbi->ll_mdc_conn, src, dir, name,
349 ptlrpc_free_req(request);
354 int ll_mdc_rename(struct inode *src, struct inode *tgt,
355 struct dentry *old, struct dentry *new)
357 struct ptlrpc_request *request = NULL;
358 struct ll_sb_info *sbi = ll_i2sbi(src);
363 err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
364 old->d_name.name, old->d_name.len,
365 new->d_name.name, new->d_name.len, &request);
366 ptlrpc_free_req(request);
372 * By the time this is called, we already have created
373 * the directory cache entry for the new file, but it
374 * is so far negative - it has no inode.
376 * If the create succeeds, we fill in the inode information
377 * with d_instantiate().
380 static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
385 struct lov_stripe_md *smd;
386 struct ll_inode_info *ii = NULL;
389 if (dentry->d_it->it_disposition == 0) {
390 memset(&oa, 0, sizeof(oa));
391 oa.o_valid = OBD_MD_FLMODE;
392 oa.o_mode = S_IFREG | 0600;
393 rc = obd_create(ll_i2obdconn(dir), &oa, &smd);
394 CDEBUG(D_DENTRY, "name %s mode %o o_id %lld: rc = %d\n",
395 dentry->d_name.name, mode, (long long)oa.o_id, rc);
400 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
401 NULL, 0, mode, 0, dentry->d_it, smd);
405 CERROR("error creating MDS object for id %Ld: rc = %d\n",
406 (unsigned long long)oa.o_id, rc);
407 #warning FIXME: 'ii' needs to be set before this goto
408 GOTO(out_destroy, rc);
411 if (dentry->d_it->it_disposition) {
412 ii = ll_i2info(inode);
413 memcpy(&ii->lli_intent_lock_handle,
414 dentry->d_it->it_lock_handle,
415 sizeof(struct lustre_handle));
416 d_instantiate(dentry, inode);
418 /* no directory data updates when intents rule */
419 rc = ext2_add_nondir(dentry, inode);
425 oa.o_easize = ii->lli_smd->lmd_easize;
426 err = obd_destroy(ll_i2obdconn(dir), &oa, ii->lli_smd);
428 CERROR("error destroying object %Ld in error path: err = %d\n",
429 (unsigned long long)oa.o_id, err);
433 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
439 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
440 NULL, 0, mode, rdev, dentry->d_it, NULL);
443 RETURN(PTR_ERR(inode));
445 /* no directory data updates when intents rule */
446 if (dentry->d_it->it_disposition == 0)
447 err = ext2_add_nondir(dentry, inode);
449 d_instantiate(dentry, inode);
454 static int ll_symlink(struct inode *dir, struct dentry *dentry,
457 unsigned l = strlen(symname);
459 struct ll_inode_info *oinfo;
461 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
462 symname, l, S_IFLNK | S_IRWXUGO, 0,
465 RETURN(PTR_ERR(inode));
467 oinfo = ll_i2info(inode);
469 if (dentry->d_it->it_disposition) {
470 #warning FIXME: still needs intent support
471 d_instantiate(dentry, inode);
475 OBD_ALLOC(oinfo->lli_symlink_name, l + 1);
476 if (!oinfo->lli_symlink_name)
479 memcpy(oinfo->lli_symlink_name, symname, l + 1);
482 return ext2_add_nondir(dentry, inode);
485 static int ll_link(struct dentry *old_dentry, struct inode * dir,
486 struct dentry *dentry)
489 struct inode *inode = old_dentry->d_inode;
491 #warning FIXME: still needs intent support
492 if (S_ISDIR(inode->i_mode))
495 if (inode->i_nlink >= EXT2_LINK_MAX)
498 err = ll_mdc_link(old_dentry, dir,
499 dentry->d_name.name, dentry->d_name.len);
503 inode->i_ctime = CURRENT_TIME;
504 ext2_inc_count(inode);
505 atomic_inc(&inode->i_count);
507 return ext2_add_nondir(dentry, inode);
510 static int ll_mkdir(struct inode * dir, struct dentry * dentry, int mode)
512 struct inode * inode;
516 if (dir->i_nlink >= EXT2_LINK_MAX)
521 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
522 NULL, 0, S_IFDIR | mode, 0, dentry->d_it, NULL);
523 err = PTR_ERR(inode);
527 ext2_inc_count(inode);
529 err = ext2_make_empty(inode, dir);
533 /* no directory data updates when intents rule */
534 if (dentry->d_it->it_disposition == 0) {
535 err = ll_add_link(dentry, inode);
540 d_instantiate(dentry, inode);
546 ext2_dec_count(inode);
547 ext2_dec_count(inode);
556 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
559 struct inode * inode = dentry->d_inode;
560 struct ext2_dir_entry_2 * de;
564 if (dentry->d_it && dentry->d_it->it_disposition) {
565 err = dentry->d_it->it_status;
571 de = ext2_find_entry(dir, dentry, &page);
575 err = ll_mdc_unlink(dir, dentry->d_inode, mode,
576 dentry->d_name.name, dentry->d_name.len);
580 err = ext2_delete_entry(de, page);
584 inode->i_ctime = dir->i_ctime;
585 ext2_dec_count(inode);
590 static int ll_unlink(struct inode *dir, struct dentry *dentry)
592 return ll_common_unlink(dir, dentry, S_IFREG);
595 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
597 struct inode * inode = dentry->d_inode;
601 if (!dentry->d_it || dentry->d_it->it_disposition == 0) {
602 if (!ext2_empty_dir(inode))
605 err = ll_common_unlink(dir, dentry, S_IFDIR);
607 err = dentry->d_it->it_status;
611 ext2_dec_count(inode);
616 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
617 struct inode * new_dir, struct dentry * new_dentry)
619 struct inode * old_inode = old_dentry->d_inode;
620 struct inode * new_inode = new_dentry->d_inode;
621 struct page * dir_page = NULL;
622 struct ext2_dir_entry_2 * dir_de = NULL;
623 struct page * old_page;
624 struct ext2_dir_entry_2 * old_de;
627 if (new_dentry->d_it && new_dentry->d_it->it_disposition)
628 GOTO(out, err = new_dentry->d_it->it_status);
630 err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
634 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
638 if (S_ISDIR(old_inode->i_mode)) {
640 dir_de = ext2_dotdot(old_inode, &dir_page);
646 struct page *new_page;
647 struct ext2_dir_entry_2 *new_de;
650 if (dir_de && !ext2_empty_dir (new_inode))
654 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
657 ext2_inc_count(old_inode);
658 ext2_set_link(new_dir, new_de, new_page, old_inode);
659 new_inode->i_ctime = CURRENT_TIME;
661 new_inode->i_nlink--;
662 ext2_dec_count(new_inode);
666 if (new_dir->i_nlink >= EXT2_LINK_MAX)
669 ext2_inc_count(old_inode);
670 err = ll_add_link(new_dentry, old_inode);
672 ext2_dec_count(old_inode);
676 ext2_inc_count(new_dir);
679 ext2_delete_entry (old_de, old_page);
680 ext2_dec_count(old_inode);
683 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
684 ext2_dec_count(old_dir);
691 page_cache_release(dir_page);
695 page_cache_release(old_page);
700 struct inode_operations ll_dir_inode_operations = {