Whamcloud - gitweb
ec8dae8ffe4c28d3590fb0fa26831abaaece943d
[fs/lustre-release.git] / lustre / llite / namei.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * This code is issued under the GNU General Public License.
5  * See the file COPYING in this distribution
6  *
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)
11  *
12  *  from
13  *
14  *  linux/fs/ext2/namei.c
15  *
16  *  Copyright (C) 1991, 1992  Linus Torvalds
17  *
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
22  *
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.
27  *
28  */
29
30 #include <linux/fs.h>
31 #include <linux/sched.h>
32 #include <linux/mm.h>
33 #include <linux/smp_lock.h>
34 #include <linux/quotaops.h>
35 #include <linux/highmem.h>
36 #include <linux/pagemap.h>
37
38 #define DEBUG_SUBSYSTEM S_LLITE
39
40 #include <linux/obd_support.h>
41 #include <linux/lustre_lite.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/obd_lov.h>
44
45 extern struct address_space_operations ll_aops;
46
47 /* from super.c */
48 extern void ll_change_inode(struct inode *inode);
49 extern int ll_setattr(struct dentry *de, struct iattr *attr);
50
51 /* from dir.c */
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);
62
63 /*
64  * Couple of helper functions - make the code slightly cleaner.
65  */
66 static inline void ext2_inc_count(struct inode *inode)
67 {
68         inode->i_nlink++;
69 }
70
71 /* postpone the disk update until the inode really goes away */
72 static inline void ext2_dec_count(struct inode *inode)
73 {
74         inode->i_nlink--;
75 }
76
77 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
78 {
79         int err;
80         err = ll_add_link(dentry, inode);
81         if (!err) {
82                 d_instantiate(dentry, inode);
83                 return 0;
84         }
85         ext2_dec_count(inode);
86         iput(inode);
87         return err;
88 }
89
90 /* methods */
91
92 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
93 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
94 #else
95 static int ll_test_inode(struct inode *inode, void *opaque)
96 #endif
97 {
98         struct ll_read_inode2_cookie *lic = opaque;
99         struct mds_body *body = lic->lic_body;
100
101         if (inode->i_generation != lic->lic_body->generation)
102                 return 0;
103
104         /* Apply the attributes in 'opaque' to this inode */
105         ll_update_inode(inode, body);
106
107         return 1;
108 }
109
110 extern struct dentry_operations ll_d_ops;
111
112 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
113 {
114         ENTRY;
115
116         ldlm_lock_decref(lockh, mode);
117
118         RETURN(0);
119 }
120
121 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
122 extern int ll_read_inode2(struct inode *inode, void *opaque);
123 struct inode *ll_iget(struct super_block *sb, ino_t hash,
124                       struct ll_read_inode2_cookie *lic)
125 {
126         struct inode *inode;
127
128         inode = iget5_locked(sb, hash, ll_test_inode, ll_read_inode2, lic);
129
130         if (!inode)
131                 return ERR_PTR(-ENOMEM);
132
133         if (inode->i_state & I_NEW) {
134
135                 unlock_new_inode(inode);
136         }
137
138         // XXX Coda always fills inodes, should Lustre?
139         return inode;
140 }
141 #else
142 struct inode *ll_iget(struct super_block *sb, ino_t hash,
143                       struct ll_read_inode2_cookie *lic)
144 {
145         struct inode *inode;
146         inode = iget4(sb, hash, ll_find_inode, lic);
147         return inode;
148 }
149 #endif
150
151 static int ll_intent_to_lock_mode(struct lookup_intent *it)
152 {
153         /* CREAT needs to be tested before open (both could be set) */
154         if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD))) {
155                 return LCK_PW;
156         } else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
157                                 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
158                                 IT_LINK | IT_LINK2 | IT_LOOKUP | IT_SYMLINK)) {
159                 return LCK_PR;
160         }
161
162         LBUG();
163         RETURN(-EINVAL);
164 }
165
166 #define LL_LOOKUP_POSITIVE 1
167 #define LL_LOOKUP_NEGATIVE 2
168
169 int ll_intent_lock(struct inode *parent, struct dentry **de,
170                    struct lookup_intent *it,
171                    intent_finish_cb intent_finish)
172 {
173         struct dentry *dentry = *de;
174         struct ll_sb_info *sbi = ll_i2sbi(parent);
175         struct lustre_handle lockh;
176         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
177         struct ptlrpc_request *request = NULL;
178         char *tgt = NULL;
179         int rc, lock_mode, tgtlen = 0, offset, flag = LL_LOOKUP_POSITIVE;
180         obd_id ino = 0;
181
182         ENTRY;
183
184         if (it == NULL)
185                 it = &lookup_it;
186
187         CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
188                dentry->d_name.name, ldlm_it2str(it->it_op));
189
190         if (dentry->d_name.len > EXT2_NAME_LEN)
191                 RETURN(-ENAMETOOLONG);
192
193         lock_mode = ll_intent_to_lock_mode(it);
194         if (it->it_op & IT_SYMLINK) {
195                 tgt = it->it_data;
196                 tgtlen = strlen(tgt);
197                 it->it_data = NULL;
198         }
199
200         rc = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_PLAIN, it, lock_mode, parent,
201                          dentry, &lockh, tgt, tgtlen, parent, sizeof(*parent));
202         if (rc < 0)
203                 RETURN(rc);
204         memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
205
206         request = (struct ptlrpc_request *)it->it_data;
207         /* it_disposition == 1 indicates that the server performed the
208          * intent on our behalf. */
209         if (it->it_disposition) {
210                 struct mds_body *mds_body;
211                 int mode, symlen = 0;
212                 obd_flag valid;
213
214                 /* This long block is all about fixing up the local
215                  * state so that it is correct as of the moment
216                  * _before_ the operation was applied; that way, the
217                  * VFS will think that everything is normal and call
218                  * Lustre's regular FS function.
219                  *
220                  * If we're performing a creation, that means that unless the
221                  * creation failed with EEXIST, we should fake up a negative
222                  * dentry.  Likewise for the target of a hard link.
223                  *
224                  * For everything else, we want to lookup to succeed. */
225
226                 /* One additional note: we add an extra reference to
227                  * the request because we need to keep it around until
228                  * ll_create gets called.  For anything else which
229                  * results in LL_LOOKUP_POSITIVE, we can do the iget()
230                  * immediately with the contents of the reply (in the
231                  * intent_finish callback).  In the create case,
232                  * however, we need to wait until ll_create_node to do
233                  * the iget() or the VFS will abort with -EEXISTS. 
234                  */
235
236                 offset = 1;
237                 mds_body = lustre_msg_buf(request->rq_repmsg, offset);
238                 ino = mds_body->fid1.id;
239                 mode = mds_body->mode;
240
241                 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
242                         mdc_store_inode_generation(request, 2, 1);
243                         /* For create ops, we want the lookup to be negative,
244                          * unless the create failed in a way that indicates
245                          * that the file is already there */
246                         if (it->it_status != -EEXIST) {
247                                 atomic_inc(&request->rq_refcount);
248                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
249                         }
250                         /*
251                          * Fall through to update attibutes: it may already
252                          * have appeared in the namespace of another client
253                          */
254                 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
255                                         IT_READLINK)) {
256                         /* For check ops, we want the lookup to succeed */
257                         it->it_data = NULL;
258                         if (it->it_status)
259                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
260                         /* Fall through to update attibutes. */
261                 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
262                         /* For rename, we want the source lookup to succeed */
263                         if (it->it_status) {
264                                 it->it_data = NULL;
265                                 GOTO(drop_req, rc = it->it_status);
266                         }
267                         it->it_data = dentry;
268                         /* Fall through to update attibutes. */
269                 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
270                         /* For remove ops, we want the lookup to succeed unless
271                          * the file truly doesn't exist */
272                         it->it_data = NULL;
273                         if (it->it_status == -ENOENT)
274                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
275                         /* No point in updating attributes that we're about to
276                          * unlink.  -phil */
277                         GOTO(out, flag = LL_LOOKUP_POSITIVE);
278                 } else if (it->it_op == IT_OPEN) {
279                         it->it_data = NULL;
280                         if (it->it_status && it->it_status != -EEXIST)
281                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
282                         /* Fall through to update attibutes. */
283                 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
284                         it->it_data = NULL;
285                         /* This means the target lookup is negative */
286                         if (mds_body->valid == 0)
287                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
288                         /* XXX bug 289: should we maybe fall through here? -p */
289                         GOTO(out, flag = LL_LOOKUP_POSITIVE);
290                 }
291
292                 /* Do a getattr now that we have the lock */
293                 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
294                 if (it->it_op == IT_READLINK) {
295                         valid |= OBD_MD_LINKNAME;
296                         symlen = mds_body->size;
297                 }
298                 ptlrpc_req_finished(request);
299                 request = NULL;
300                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
301                                  valid, symlen, &request);
302                 if (rc) {
303                         CERROR("failure %d inode "LPX64"\n", rc, ino);
304                         GOTO(drop_req, rc = -abs(rc));
305                 }
306                 offset = 0;
307         } else {
308                 struct ll_inode_info *lli = ll_i2info(parent);
309                 int mode;
310
311                 /* it_disposition == 0 indicates that it just did a simple lock
312                  * request, for which we are very thankful.  move along with
313                  * the local lookup then. */
314
315                 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
316                 offset = 0;
317
318                 ino = ll_inode_by_name(parent, dentry, &mode);
319                 if (!ino) {
320                         CERROR("inode %*s not found by name\n",
321                                dentry->d_name.len, dentry->d_name.name);
322                         GOTO(drop_lock, rc = -ENOENT);
323                 }
324
325                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
326                                  OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
327                 if (rc) {
328                         CERROR("failure %d inode "LPX64"\n", rc, ino);
329                         GOTO(drop_req, rc = -abs(rc));
330                 }
331         }
332
333         EXIT;
334  out:
335         if (intent_finish != NULL) {
336                 rc = intent_finish(flag, request, de, it, offset, ino);
337                 dentry = *de; /* intent_finish may change *de */
338         } else {
339                 ptlrpc_req_finished(request);
340         }
341
342         /* this places the intent in the dentry so that the vfs_xxx
343          *  operation can lay its hands on it; but that is not 
344          *  always needed...
345          */
346         if (it->it_status == 0 && 
347             it->it_op != IT_RENAME2 && 
348             it->it_op != IT_SETATTR &&
349             it->it_op != IT_GETATTR &&
350             it->it_op != IT_READDIR &&
351             it->it_op != IT_LOOKUP) {
352                 LL_SAVE_INTENT(dentry, it);
353         } else {
354                 dentry->d_it = NULL;
355                 CDEBUG(D_DENTRY,
356                        "D_IT dentry %p fsdata %p intent: %s status %d\n",
357                        dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
358                        it->it_status);
359         }
360
361         if (rc < 0 || it->it_op == IT_LOOKUP)
362                 ll_intent_release(dentry, it);
363
364         return rc;
365
366  drop_req:
367         ptlrpc_free_req(request);
368  drop_lock:
369 #warning FIXME: must release lock here
370         return rc;
371 }
372
373 /* Search "inode"'s alias list for a dentry that has the same name and parent as
374  * de.  If found, return it.  If not found, return de. */
375 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
376 {
377         struct list_head *tmp;
378
379         spin_lock(&dcache_lock);
380         list_for_each(tmp, &inode->i_dentry) {
381                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
382
383                 /* We are called here with 'de' already on the aliases list. */
384                 if (dentry == de) { 
385                         CERROR("whoops\n");
386                         continue;
387                 }
388
389                 if (dentry->d_parent != de->d_parent)
390                         continue;
391
392                 if (dentry->d_name.len != de->d_name.len)
393                         continue;
394
395                 if (memcmp(dentry->d_name.name, de->d_name.name,
396                            de->d_name.len) != 0)
397                         continue;
398
399                 if (!list_empty(&dentry->d_lru))
400                         list_del_init(&dentry->d_lru);
401
402                 list_del_init(&dentry->d_hash);
403                 spin_unlock(&dcache_lock);
404                 d_rehash(dentry);
405                 atomic_inc(&dentry->d_count);
406                 iput(inode);
407                 return dentry;
408         }
409
410         spin_unlock(&dcache_lock);
411
412         return de;
413 }
414
415 static int
416 lookup2_finish(int flag, struct ptlrpc_request *request, struct dentry **de,
417                struct lookup_intent *it, int offset, obd_id ino)
418 {
419         struct dentry *dentry = *de, *saved = *de;
420         struct inode *inode = NULL;
421         struct ll_read_inode2_cookie lic;
422
423         if (flag == LL_LOOKUP_POSITIVE) {
424                 ENTRY;
425                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
426
427                 if (S_ISREG(lic.lic_body->mode) &&
428                     lic.lic_body->valid & OBD_MD_FLEASIZE) {
429                         LASSERT(request->rq_repmsg->bufcount > offset);
430                         lic.lic_lmm = lustre_msg_buf(request->rq_repmsg,
431                                                      offset + 1);
432                 } else {
433                         lic.lic_lmm = NULL;
434                 }
435
436                 /* No rpc's happen during iget4, -ENOMEM's are possible */
437                 LASSERT(ino != 0);
438                 inode = ll_iget(dentry->d_sb, ino, &lic);
439                 if (!inode) {
440                         /* XXX make sure that request is freed in this case;
441                          * I think it is, but double-check refcounts. -phil */
442                         RETURN(-ENOMEM);
443                 }
444
445                 dentry = *de = ll_find_alias(inode, dentry);
446
447                 /* We asked for a lock on the directory, and may have been
448                  * granted a lock on the inode.  Just in case, fixup the data
449                  * pointer. */
450                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
451                                    inode, sizeof(*inode));
452
453                 EXIT;
454         } else {
455                 ENTRY;
456         }
457
458         ptlrpc_req_finished(request);
459
460         dentry->d_op = &ll_d_ops;
461         if (ll_d2d(dentry) == NULL) {
462                 ll_set_dd(dentry);
463         }
464
465         if (dentry == saved)
466                 d_add(dentry, inode);
467
468         RETURN(0);
469 }
470
471 static struct dentry *ll_lookup2(struct inode *parent, struct dentry *dentry,
472                                  struct lookup_intent *it)
473 {
474         struct dentry *save = dentry;
475         int rc;
476
477         rc = ll_intent_lock(parent, &dentry, it, lookup2_finish);
478         if (rc < 0) {
479                 CERROR("ll_intent_lock: %d\n", rc);
480                 return ERR_PTR(rc);
481         }
482
483         if (dentry == save)
484                 return NULL;
485         else
486                 return dentry;
487 }
488
489 static struct inode *ll_create_node(struct inode *dir, const char *name,
490                                     int namelen, const char *tgt, int tgtlen,
491                                     int mode, __u64 extra,
492                                     struct lookup_intent *it,
493                                     struct lov_stripe_md *lsm)
494 {
495         struct inode *inode;
496         struct ptlrpc_request *request = NULL;
497         struct mds_body *body;
498         time_t time = CURRENT_TIME;
499         struct ll_sb_info *sbi = ll_i2sbi(dir);
500         struct ll_read_inode2_cookie lic;
501         struct lov_mds_md *lmm = NULL;
502         ENTRY;
503
504         if (it && it->it_disposition) {
505                 int rc = it->it_status;
506                 if (rc) {
507                         CERROR("error creating MDS inode for %*s: rc = %d\n",
508                                namelen, name, rc);
509                         RETURN(ERR_PTR(rc));
510                 }
511                 ll_invalidate_inode_pages(dir);
512                 request = it->it_data;
513                 body = lustre_msg_buf(request->rq_repmsg, 1);
514                 lic.lic_lmm = NULL;
515         } else {
516                 int gid = current->fsgid;
517                 int rc;
518
519                 if (lsm) {
520                         OBD_ALLOC(lmm, lsm->lsm_mds_easize);
521                         if (!lmm)
522                                 RETURN(ERR_PTR(-ENOMEM));
523                         lov_packmd(lmm, lsm);
524                         lic.lic_lmm = lmm;
525                 } else
526                         lic.lic_lmm = NULL;
527
528                 if (dir->i_mode & S_ISGID) {
529                         gid = dir->i_gid;
530                         if (S_ISDIR(mode))
531                                 mode |= S_ISGID;
532                 }
533
534                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
535                                 tgtlen, mode, current->fsuid, gid,
536                                 time, extra, lsm, &request);
537                 if (rc) {
538                         inode = ERR_PTR(rc);
539                         GOTO(out, rc);
540                 }
541                 body = lustre_msg_buf(request->rq_repmsg, 0);
542         }
543
544         lic.lic_body = body;
545
546         LASSERT(body->ino != 0);
547         inode = ll_iget(dir->i_sb, body->ino, &lic);
548         if (IS_ERR(inode)) {
549                 int rc = PTR_ERR(inode);
550                 CERROR("new_inode -fatal: rc %d\n", rc);
551                 LBUG();
552                 GOTO(out, rc);
553         }
554
555         if (!list_empty(&inode->i_dentry)) {
556                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
557                        body->ino, atomic_read(&inode->i_count),
558                        inode->i_nlink);
559                 iput(inode);
560                 LBUG();
561                 inode = ERR_PTR(-EIO);
562                 GOTO(out, -EIO);
563         }
564
565         if (it && it->it_disposition) {
566                 /* We asked for a lock on the directory, but were
567                  * granted a lock on the inode.  Since we finally have
568                  * an inode pointer, stuff it in the lock. */
569                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
570                                    inode, sizeof(*inode));
571         }
572
573         EXIT;
574  out:
575         if (lsm && lmm)
576                 OBD_FREE(lmm, lsm->lsm_mds_easize);
577         ptlrpc_req_finished(request);
578         return inode;
579 }
580
581 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
582                          const char *name, int len)
583 {
584         struct ptlrpc_request *request = NULL;
585         struct ll_sb_info *sbi = ll_i2sbi(dir);
586         int err;
587
588         ENTRY;
589
590         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
591                          &request);
592         ptlrpc_req_finished(request);
593
594         RETURN(err);
595 }
596
597 int ll_mdc_link(struct dentry *src, struct inode *dir,
598                 const char *name, int len)
599 {
600         struct ptlrpc_request *request = NULL;
601         int err;
602         struct ll_sb_info *sbi = ll_i2sbi(dir);
603
604         ENTRY;
605
606         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
607         ptlrpc_req_finished(request);
608
609         RETURN(err);
610 }
611
612 int ll_mdc_rename(struct inode *src, struct inode *tgt,
613                   struct dentry *old, struct dentry *new)
614 {
615         struct ptlrpc_request *request = NULL;
616         struct ll_sb_info *sbi = ll_i2sbi(src);
617         int err;
618
619         ENTRY;
620
621         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
622                          old->d_name.name, old->d_name.len,
623                          new->d_name.name, new->d_name.len, &request);
624         ptlrpc_req_finished(request);
625
626         RETURN(err);
627 }
628
629 /*
630  * By the time this is called, we already have created the directory cache
631  * entry for the new file, but it is so far negative - it has no inode.
632  * We defer creating the OBD object(s) until open, to keep the intent and
633  * non-intent code paths similar, and also because we do not have the MDS
634  * inode number before calling ll_create_node() (which is needed for LOV),
635  * so we would need to do yet another RPC to the MDS to store the LOV EA
636  * data on the MDS.
637  *
638  * If the create succeeds, we fill in the inode information
639  * with d_instantiate().
640  */
641 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
642 {
643         struct lookup_intent *it;
644         struct inode *inode;
645         int rc = 0;
646         ENTRY;
647
648         LL_GET_INTENT(dentry, it);
649
650         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
651                                NULL, 0, mode, 0, it, NULL);
652
653         if (IS_ERR(inode))
654                 RETURN(PTR_ERR(inode));
655
656         if (it && it->it_disposition) {
657                 struct ll_inode_info *lli = ll_i2info(inode);
658                 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
659                        sizeof(lli->lli_intent_lock_handle));
660                 d_instantiate(dentry, inode);
661         } else {
662                 /* no directory data updates when intents rule */
663                 rc = ext2_add_nondir(dentry, inode);
664         }
665
666         RETURN(rc);
667 }
668
669 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
670                     int rdev)
671 {
672         struct lookup_intent *it;
673         struct inode *inode;
674         int rc = 0;
675
676         LL_GET_INTENT(dentry, it);
677
678         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
679                                NULL, 0, mode, rdev, it, NULL);
680
681         if (IS_ERR(inode))
682                 RETURN(PTR_ERR(inode));
683
684         /* no directory data updates when intents rule */
685         if (it && it->it_disposition)
686                 d_instantiate(dentry, inode);
687         else
688                 rc = ext2_add_nondir(dentry, inode);
689
690         return rc;
691 }
692
693 static int ll_symlink(struct inode *dir, struct dentry *dentry,
694                       const char *symname)
695 {
696         struct lookup_intent *it;
697         unsigned l = strlen(symname);
698         struct inode *inode;
699         struct ll_inode_info *lli;
700         int err = 0;
701         ENTRY;
702
703         LL_GET_INTENT(dentry, it);
704
705         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
706                                symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
707         if (IS_ERR(inode))
708                 RETURN(PTR_ERR(inode));
709
710         lli = ll_i2info(inode);
711
712         OBD_ALLOC(lli->lli_symlink_name, l + 1);
713         /* this _could_ be a non-fatal error, since the symlink is already
714          * stored on the MDS by this point, and we can re-get it in readlink.
715          */
716         if (!lli->lli_symlink_name)
717                 RETURN(-ENOMEM);
718
719         memcpy(lli->lli_symlink_name, symname, l + 1);
720         inode->i_size = l;
721
722         /* no directory data updates when intents rule */
723         if (it && it->it_disposition)
724                 d_instantiate(dentry, inode);
725         else
726                 err = ext2_add_nondir(dentry, inode);
727
728         RETURN(err);
729 }
730
731 static int ll_link(struct dentry *old_dentry, struct inode * dir,
732                    struct dentry *dentry)
733 {
734         struct lookup_intent *it;
735         struct inode *inode = old_dentry->d_inode;
736         int rc;
737
738         LL_GET_INTENT(dentry, it);
739
740         if (it && it->it_disposition) {
741                 if (it->it_status)
742                         RETURN(it->it_status);
743                 inode->i_ctime = CURRENT_TIME;
744                 ext2_inc_count(inode);
745                 atomic_inc(&inode->i_count);
746                 d_instantiate(dentry, inode);
747                 ll_invalidate_inode_pages(dir);
748                 RETURN(0);
749         }
750
751         if (S_ISDIR(inode->i_mode))
752                 return -EPERM;
753
754         if (inode->i_nlink >= EXT2_LINK_MAX)
755                 return -EMLINK;
756
757         rc = ll_mdc_link(old_dentry, dir,
758                           dentry->d_name.name, dentry->d_name.len);
759         if (rc)
760                 RETURN(rc);
761
762         inode->i_ctime = CURRENT_TIME;
763         ext2_inc_count(inode);
764         atomic_inc(&inode->i_count);
765
766         return ext2_add_nondir(dentry, inode);
767 }
768
769 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
770 {
771         struct lookup_intent *it;
772         struct inode * inode;
773         int err = -EMLINK;
774         ENTRY;
775
776         LL_GET_INTENT(dentry, it);
777
778         if (dir->i_nlink >= EXT2_LINK_MAX)
779                 goto out;
780
781         ext2_inc_count(dir);
782         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
783                                NULL, 0, S_IFDIR | mode, 0, it, NULL);
784         err = PTR_ERR(inode);
785         if (IS_ERR(inode))
786                 goto out_dir;
787
788         err = ext2_make_empty(inode, dir);
789         if (err)
790                 goto out_fail;
791
792         /* no directory data updates when intents rule */
793         if (!it || !it->it_disposition) {
794                 /* XXX FIXME This code needs re-checked for non-intents */
795                 ext2_inc_count(inode);
796                 err = ll_add_link(dentry, inode);
797                 if (err)
798                         goto out_fail;
799         }
800
801         d_instantiate(dentry, inode);
802 out:
803         EXIT;
804         return err;
805
806 out_fail:
807         ext2_dec_count(inode);
808         ext2_dec_count(inode);
809         iput(inode);
810         EXIT;
811 out_dir:
812         ext2_dec_count(dir);
813         EXIT;
814         goto out;
815 }
816
817 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
818                             struct lookup_intent *it, __u32 mode)
819 {
820         struct inode *inode = dentry->d_inode;
821         struct ext2_dir_entry_2 * de;
822         struct page * page;
823         int rc = 0;
824
825         if (it && it->it_disposition) {
826                 rc = it->it_status;
827                 ll_invalidate_inode_pages(dir);
828                 if (rc)
829                         GOTO(out, rc);
830                 GOTO(out_dec, 0);
831         }
832
833         de = ext2_find_entry(dir, dentry, &page);
834         if (!de)
835                 GOTO(out, rc = -ENOENT);
836         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
837                            dentry->d_name.name, dentry->d_name.len);
838         if (rc)
839                 GOTO(out, rc);
840
841         rc = ext2_delete_entry(de, page);
842         if (rc)
843                 GOTO(out, rc);
844
845         /* AED: not sure if needed - directory lock revocation should do it
846          * in the case where the client has cached it for non-intent ops.
847          */
848         ll_invalidate_inode_pages(dir);
849
850         inode->i_ctime = dir->i_ctime;
851 out_dec:
852         ext2_dec_count(inode);
853 out:
854         return rc;
855 }
856
857 static int ll_unlink(struct inode *dir, struct dentry *dentry)
858 {
859         struct lookup_intent * it;
860
861         LL_GET_INTENT(dentry, it);
862
863         return ll_common_unlink(dir, dentry, it, S_IFREG);
864 }
865
866 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
867 {
868         struct inode * inode = dentry->d_inode;
869         struct lookup_intent *it;
870         int rc;
871         ENTRY;
872
873         LL_GET_INTENT(dentry, it);
874
875         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
876                 RETURN(-ENOTEMPTY);
877
878         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
879         if (!rc) {
880                 inode->i_size = 0;
881                 ext2_dec_count(inode);
882                 ext2_dec_count(dir);
883         }
884
885         RETURN(rc);
886 }
887
888 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
889                      struct inode * new_dir, struct dentry * new_dentry)
890 {
891         struct lookup_intent *it;
892         struct inode * old_inode = old_dentry->d_inode;
893         struct inode * tgt_inode = new_dentry->d_inode;
894         struct page * dir_page = NULL;
895         struct ext2_dir_entry_2 * dir_de = NULL;
896         struct ext2_dir_entry_2 * old_de;
897         struct page * old_page;
898         int err;
899
900         LL_GET_INTENT(old_dentry, it);
901
902         if (it && it->it_disposition) {
903                 if (tgt_inode) {
904                         tgt_inode->i_ctime = CURRENT_TIME;
905                         tgt_inode->i_nlink--;
906                 }
907                 ll_invalidate_inode_pages(old_dir);
908                 ll_invalidate_inode_pages(new_dir);
909                 GOTO(out, err = it->it_status);
910         }
911
912         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
913         if (err)
914                 goto out;
915
916         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
917         if (!old_de)
918                 goto out;
919
920         if (S_ISDIR(old_inode->i_mode)) {
921                 err = -EIO;
922                 dir_de = ext2_dotdot(old_inode, &dir_page);
923                 if (!dir_de)
924                         goto out_old;
925         }
926
927         if (tgt_inode) {
928                 struct page *new_page;
929                 struct ext2_dir_entry_2 *new_de;
930
931                 err = -ENOTEMPTY;
932                 if (dir_de && !ext2_empty_dir (tgt_inode))
933                         goto out_dir;
934
935                 err = -ENOENT;
936                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
937                 if (!new_de)
938                         goto out_dir;
939                 ext2_inc_count(old_inode);
940                 ext2_set_link(new_dir, new_de, new_page, old_inode);
941                 tgt_inode->i_ctime = CURRENT_TIME;
942                 if (dir_de)
943                         tgt_inode->i_nlink--;
944                 ext2_dec_count(tgt_inode);
945         } else {
946                 if (dir_de) {
947                         err = -EMLINK;
948                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
949                                 goto out_dir;
950                 }
951                 ext2_inc_count(old_inode);
952                 err = ll_add_link(new_dentry, old_inode);
953                 if (err) {
954                         ext2_dec_count(old_inode);
955                         goto out_dir;
956                 }
957                 if (dir_de)
958                         ext2_inc_count(new_dir);
959         }
960
961         ext2_delete_entry (old_de, old_page);
962         ext2_dec_count(old_inode);
963
964         if (dir_de) {
965                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
966                 ext2_dec_count(old_dir);
967         }
968         return 0;
969
970 out_dir:
971         if (dir_de) {
972                 kunmap(dir_page);
973                 page_cache_release(dir_page);
974         }
975 out_old:
976         kunmap(old_page);
977         page_cache_release(old_page);
978 out:
979         return err;
980 }
981
982 struct inode_operations ll_dir_inode_operations = {
983         create:         ll_create,
984         lookup2:        ll_lookup2,
985         link:           ll_link,
986         unlink:         ll_unlink,
987         symlink:        ll_symlink,
988         mkdir:          ll_mkdir,
989         rmdir:          ll_rmdir,
990         mknod:          ll_mknod,
991         rename:         ll_rename,
992         setattr:        ll_setattr
993 };