Whamcloud - gitweb
merging all noncontroversial pieces of b_symlink into HEAD
[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 || 
362             it->it_op == IT_LOOKUP
363             )
364                 ll_intent_release(dentry, it);
365
366         return rc;
367
368  drop_req:
369         ptlrpc_free_req(request);
370  drop_lock:
371 #warning FIXME: must release lock here
372         return rc;
373 }
374
375 /* Search "inode"'s alias list for a dentry that has the same name and parent as
376  * de.  If found, return it.  If not found, return de. */
377 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
378 {
379         struct list_head *tmp;
380
381         spin_lock(&dcache_lock);
382         list_for_each(tmp, &inode->i_dentry) {
383                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
384
385                 /* We are called here with 'de' already on the aliases list. */
386                 if (dentry == de)
387                         continue;
388
389                 if (!atomic_read(&dentry->d_count))
390                         continue;
391
392                 if (!list_empty(&dentry->d_lru))
393                         continue;
394
395                 if (dentry->d_parent != de->d_parent)
396                         continue;
397
398                 if (dentry->d_name.len != de->d_name.len)
399                         continue;
400
401                 if (memcmp(dentry->d_name.name, de->d_name.name,
402                            de->d_name.len) != 0)
403                         continue;
404
405                 list_del_init(&dentry->d_hash);
406
407                 spin_unlock(&dcache_lock);
408                 d_rehash(dentry);
409                 atomic_inc(&dentry->d_count);
410                 iput(inode);
411                 return dentry;
412         }
413
414         spin_unlock(&dcache_lock);
415
416         return de;
417 }
418
419 static int
420 lookup2_finish(int flag, struct ptlrpc_request *request, struct dentry **de,
421                struct lookup_intent *it, int offset, obd_id ino)
422 {
423         struct dentry *dentry = *de, *saved = *de;
424         struct inode *inode = NULL;
425         struct ll_read_inode2_cookie lic;
426
427         if (flag == LL_LOOKUP_POSITIVE) {
428                 ENTRY;
429                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
430
431                 if (S_ISREG(lic.lic_body->mode) &&
432                     lic.lic_body->valid & OBD_MD_FLEASIZE) {
433                         LASSERT(request->rq_repmsg->bufcount > offset);
434                         lic.lic_lmm = lustre_msg_buf(request->rq_repmsg,
435                                                      offset + 1);
436                 } else {
437                         lic.lic_lmm = NULL;
438                 }
439
440                 /* No rpc's happen during iget4, -ENOMEM's are possible */
441                 LASSERT(ino != 0);
442                 inode = ll_iget(dentry->d_sb, ino, &lic);
443                 if (!inode) {
444                         /* XXX make sure that request is freed in this case;
445                          * I think it is, but double-check refcounts. -phil */
446                         RETURN(-ENOMEM);
447                 }
448
449                 dentry = *de = ll_find_alias(inode, dentry);
450
451                 /* We asked for a lock on the directory, and may have been
452                  * granted a lock on the inode.  Just in case, fixup the data
453                  * pointer. */
454                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
455                                    inode, sizeof(*inode));
456
457                 EXIT;
458         } else {
459                 ENTRY;
460         }
461
462         ptlrpc_req_finished(request);
463
464         dentry->d_op = &ll_d_ops;
465         if (ll_d2d(dentry) == NULL) {
466                 ll_set_dd(dentry);
467         }
468
469         if (dentry == saved)
470                 d_add(dentry, inode);
471
472         RETURN(0);
473 }
474
475 static struct dentry *ll_lookup2(struct inode *parent, struct dentry *dentry,
476                                  struct lookup_intent *it)
477 {
478         struct dentry *save = dentry;
479         int rc;
480
481         rc = ll_intent_lock(parent, &dentry, it, lookup2_finish);
482         if (rc < 0) {
483                 CERROR("ll_intent_lock: %d\n", rc);
484                 return ERR_PTR(rc);
485         }
486
487         if (dentry == save)
488                 return NULL;
489         else
490                 return dentry;
491 }
492
493 static struct inode *ll_create_node(struct inode *dir, const char *name,
494                                     int namelen, const char *tgt, int tgtlen,
495                                     int mode, __u64 extra,
496                                     struct lookup_intent *it,
497                                     struct lov_stripe_md *lsm)
498 {
499         struct inode *inode;
500         struct ptlrpc_request *request = NULL;
501         struct mds_body *body;
502         time_t time = CURRENT_TIME;
503         struct ll_sb_info *sbi = ll_i2sbi(dir);
504         struct ll_read_inode2_cookie lic;
505         struct lov_mds_md *lmm = NULL;
506         ENTRY;
507
508         if (it && it->it_disposition) {
509                 int rc = it->it_status;
510                 if (rc) {
511                         CERROR("error creating MDS inode for %*s: rc = %d\n",
512                                namelen, name, rc);
513                         RETURN(ERR_PTR(rc));
514                 }
515                 ll_invalidate_inode_pages(dir);
516                 request = it->it_data;
517                 body = lustre_msg_buf(request->rq_repmsg, 1);
518                 lic.lic_lmm = NULL;
519         } else {
520                 int gid = current->fsgid;
521                 int rc;
522
523                 if (lsm) {
524                         OBD_ALLOC(lmm, lsm->lsm_mds_easize);
525                         if (!lmm)
526                                 RETURN(ERR_PTR(-ENOMEM));
527                         lov_packmd(lmm, lsm);
528                         lic.lic_lmm = lmm;
529                 } else
530                         lic.lic_lmm = NULL;
531
532                 if (dir->i_mode & S_ISGID) {
533                         gid = dir->i_gid;
534                         if (S_ISDIR(mode))
535                                 mode |= S_ISGID;
536                 }
537
538                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
539                                 tgtlen, mode, current->fsuid, gid,
540                                 time, extra, lsm, &request);
541                 if (rc) {
542                         inode = ERR_PTR(rc);
543                         GOTO(out, rc);
544                 }
545                 body = lustre_msg_buf(request->rq_repmsg, 0);
546         }
547
548         lic.lic_body = body;
549
550         LASSERT(body->ino != 0);
551         inode = ll_iget(dir->i_sb, body->ino, &lic);
552         if (IS_ERR(inode)) {
553                 int rc = PTR_ERR(inode);
554                 CERROR("new_inode -fatal: rc %d\n", rc);
555                 LBUG();
556                 GOTO(out, rc);
557         }
558
559         if (!list_empty(&inode->i_dentry)) {
560                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
561                        body->ino, atomic_read(&inode->i_count),
562                        inode->i_nlink);
563                 iput(inode);
564                 LBUG();
565                 inode = ERR_PTR(-EIO);
566                 GOTO(out, -EIO);
567         }
568
569         if (it && it->it_disposition) {
570                 /* We asked for a lock on the directory, but were
571                  * granted a lock on the inode.  Since we finally have
572                  * an inode pointer, stuff it in the lock. */
573                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
574                                    inode, sizeof(*inode));
575         }
576
577         EXIT;
578  out:
579         if (lsm && lmm)
580                 OBD_FREE(lmm, lsm->lsm_mds_easize);
581         ptlrpc_req_finished(request);
582         return inode;
583 }
584
585 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
586                          const char *name, int len)
587 {
588         struct ptlrpc_request *request = NULL;
589         struct ll_sb_info *sbi = ll_i2sbi(dir);
590         int err;
591
592         ENTRY;
593
594         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
595                          &request);
596         ptlrpc_req_finished(request);
597
598         RETURN(err);
599 }
600
601 int ll_mdc_link(struct dentry *src, struct inode *dir,
602                 const char *name, int len)
603 {
604         struct ptlrpc_request *request = NULL;
605         int err;
606         struct ll_sb_info *sbi = ll_i2sbi(dir);
607
608         ENTRY;
609
610         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
611         ptlrpc_req_finished(request);
612
613         RETURN(err);
614 }
615
616 int ll_mdc_rename(struct inode *src, struct inode *tgt,
617                   struct dentry *old, struct dentry *new)
618 {
619         struct ptlrpc_request *request = NULL;
620         struct ll_sb_info *sbi = ll_i2sbi(src);
621         int err;
622
623         ENTRY;
624
625         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
626                          old->d_name.name, old->d_name.len,
627                          new->d_name.name, new->d_name.len, &request);
628         ptlrpc_req_finished(request);
629
630         RETURN(err);
631 }
632
633 /*
634  * By the time this is called, we already have created the directory cache
635  * entry for the new file, but it is so far negative - it has no inode.
636  * We defer creating the OBD object(s) until open, to keep the intent and
637  * non-intent code paths similar, and also because we do not have the MDS
638  * inode number before calling ll_create_node() (which is needed for LOV),
639  * so we would need to do yet another RPC to the MDS to store the LOV EA
640  * data on the MDS.
641  *
642  * If the create succeeds, we fill in the inode information
643  * with d_instantiate().
644  */
645 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
646 {
647         struct lookup_intent *it;
648         struct inode *inode;
649         int rc = 0;
650         ENTRY;
651
652         LL_GET_INTENT(dentry, it);
653
654         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
655                                NULL, 0, mode, 0, it, NULL);
656
657         if (IS_ERR(inode))
658                 RETURN(PTR_ERR(inode));
659
660         if (it && it->it_disposition) {
661                 struct ll_inode_info *lli = ll_i2info(inode);
662                 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
663                        sizeof(lli->lli_intent_lock_handle));
664                 d_instantiate(dentry, inode);
665         } else {
666                 /* no directory data updates when intents rule */
667                 rc = ext2_add_nondir(dentry, inode);
668         }
669
670         RETURN(rc);
671 }
672
673 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
674                     int rdev)
675 {
676         struct lookup_intent *it;
677         struct inode *inode;
678         int rc = 0;
679
680         LL_GET_INTENT(dentry, it);
681
682         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
683                                NULL, 0, mode, rdev, it, NULL);
684
685         if (IS_ERR(inode))
686                 RETURN(PTR_ERR(inode));
687
688         /* no directory data updates when intents rule */
689         if (it && it->it_disposition)
690                 d_instantiate(dentry, inode);
691         else
692                 rc = ext2_add_nondir(dentry, inode);
693
694         return rc;
695 }
696
697 static int ll_symlink(struct inode *dir, struct dentry *dentry,
698                       const char *symname)
699 {
700         struct lookup_intent *it;
701         unsigned l = strlen(symname);
702         struct inode *inode;
703         struct ll_inode_info *lli;
704         int err = 0;
705         ENTRY;
706
707         LL_GET_INTENT(dentry, it);
708
709         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
710                                symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
711         if (IS_ERR(inode))
712                 RETURN(PTR_ERR(inode));
713
714         lli = ll_i2info(inode);
715
716         OBD_ALLOC(lli->lli_symlink_name, l + 1);
717         /* this _could_ be a non-fatal error, since the symlink is already
718          * stored on the MDS by this point, and we can re-get it in readlink.
719          */
720         if (!lli->lli_symlink_name)
721                 RETURN(-ENOMEM);
722
723         memcpy(lli->lli_symlink_name, symname, l + 1);
724         inode->i_size = l;
725
726         /* no directory data updates when intents rule */
727         if (it && it->it_disposition)
728                 d_instantiate(dentry, inode);
729         else
730                 err = ext2_add_nondir(dentry, inode);
731
732         RETURN(err);
733 }
734
735 static int ll_link(struct dentry *old_dentry, struct inode * dir,
736                    struct dentry *dentry)
737 {
738         struct lookup_intent *it;
739         struct inode *inode = old_dentry->d_inode;
740         int rc;
741
742         LL_GET_INTENT(dentry, it);
743
744         if (it && it->it_disposition) {
745                 if (it->it_status)
746                         RETURN(it->it_status);
747                 inode->i_ctime = CURRENT_TIME;
748                 ext2_inc_count(inode);
749                 atomic_inc(&inode->i_count);
750                 d_instantiate(dentry, inode);
751                 ll_invalidate_inode_pages(dir);
752                 RETURN(0);
753         }
754
755         if (S_ISDIR(inode->i_mode))
756                 return -EPERM;
757
758         if (inode->i_nlink >= EXT2_LINK_MAX)
759                 return -EMLINK;
760
761         rc = ll_mdc_link(old_dentry, dir,
762                           dentry->d_name.name, dentry->d_name.len);
763         if (rc)
764                 RETURN(rc);
765
766         inode->i_ctime = CURRENT_TIME;
767         ext2_inc_count(inode);
768         atomic_inc(&inode->i_count);
769
770         return ext2_add_nondir(dentry, inode);
771 }
772
773 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
774 {
775         struct lookup_intent *it;
776         struct inode * inode;
777         int err = -EMLINK;
778         ENTRY;
779
780         LL_GET_INTENT(dentry, it);
781
782         if (dir->i_nlink >= EXT2_LINK_MAX)
783                 goto out;
784
785         ext2_inc_count(dir);
786         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
787                                NULL, 0, S_IFDIR | mode, 0, it, NULL);
788         err = PTR_ERR(inode);
789         if (IS_ERR(inode))
790                 goto out_dir;
791
792         ext2_inc_count(inode);
793
794         err = ext2_make_empty(inode, dir);
795         if (err)
796                 goto out_fail;
797
798         /* no directory data updates when intents rule */
799         if (!it || !it->it_disposition) {
800                 err = ll_add_link(dentry, inode);
801                 if (err)
802                         goto out_fail;
803         }
804
805         d_instantiate(dentry, inode);
806 out:
807         EXIT;
808         return err;
809
810 out_fail:
811         ext2_dec_count(inode);
812         ext2_dec_count(inode);
813         iput(inode);
814         EXIT;
815 out_dir:
816         ext2_dec_count(dir);
817         EXIT;
818         goto out;
819 }
820
821 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
822                             struct lookup_intent *it, __u32 mode)
823 {
824         struct inode *inode = dentry->d_inode;
825         struct ext2_dir_entry_2 * de;
826         struct page * page;
827         int rc = 0;
828
829         if (it && it->it_disposition) {
830                 rc = it->it_status;
831                 ll_invalidate_inode_pages(dir);
832                 if (rc)
833                         GOTO(out, rc);
834                 GOTO(out_dec, 0);
835         }
836
837         de = ext2_find_entry(dir, dentry, &page);
838         if (!de)
839                 GOTO(out, rc = -ENOENT);
840         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
841                            dentry->d_name.name, dentry->d_name.len);
842         if (rc)
843                 GOTO(out, rc);
844
845         rc = ext2_delete_entry(de, page);
846         if (rc)
847                 GOTO(out, rc);
848
849         /* AED: not sure if needed - directory lock revocation should do it
850          * in the case where the client has cached it for non-intent ops.
851          */
852         ll_invalidate_inode_pages(dir);
853
854         inode->i_ctime = dir->i_ctime;
855 out_dec:
856         ext2_dec_count(inode);
857 out:
858         return rc;
859 }
860
861 static int ll_unlink(struct inode *dir, struct dentry *dentry)
862 {
863         struct lookup_intent * it;
864
865         LL_GET_INTENT(dentry, it);
866
867         return ll_common_unlink(dir, dentry, it, S_IFREG);
868 }
869
870 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
871 {
872         struct inode * inode = dentry->d_inode;
873         struct lookup_intent *it;
874         int rc;
875         ENTRY;
876
877         LL_GET_INTENT(dentry, it);
878
879         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
880                 RETURN(-ENOTEMPTY);
881
882         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
883         if (!rc) {
884                 inode->i_size = 0;
885                 ext2_dec_count(inode);
886                 ext2_dec_count(dir);
887         }
888
889         RETURN(rc);
890 }
891
892 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
893                      struct inode * new_dir, struct dentry * new_dentry)
894 {
895         struct lookup_intent *it;
896         struct inode * old_inode = old_dentry->d_inode;
897         struct inode * tgt_inode = new_dentry->d_inode;
898         struct page * dir_page = NULL;
899         struct ext2_dir_entry_2 * dir_de = NULL;
900         struct ext2_dir_entry_2 * old_de;
901         struct page * old_page;
902         int err;
903
904         LL_GET_INTENT(old_dentry, it);
905
906         if (it && it->it_disposition) {
907                 if (tgt_inode) {
908                         tgt_inode->i_ctime = CURRENT_TIME;
909                         tgt_inode->i_nlink--;
910                 }
911                 ll_invalidate_inode_pages(old_dir);
912                 ll_invalidate_inode_pages(new_dir);
913                 GOTO(out, err = it->it_status);
914         }
915
916         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
917         if (err)
918                 goto out;
919
920         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
921         if (!old_de)
922                 goto out;
923
924         if (S_ISDIR(old_inode->i_mode)) {
925                 err = -EIO;
926                 dir_de = ext2_dotdot(old_inode, &dir_page);
927                 if (!dir_de)
928                         goto out_old;
929         }
930
931         if (tgt_inode) {
932                 struct page *new_page;
933                 struct ext2_dir_entry_2 *new_de;
934
935                 err = -ENOTEMPTY;
936                 if (dir_de && !ext2_empty_dir (tgt_inode))
937                         goto out_dir;
938
939                 err = -ENOENT;
940                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
941                 if (!new_de)
942                         goto out_dir;
943                 ext2_inc_count(old_inode);
944                 ext2_set_link(new_dir, new_de, new_page, old_inode);
945                 tgt_inode->i_ctime = CURRENT_TIME;
946                 if (dir_de)
947                         tgt_inode->i_nlink--;
948                 ext2_dec_count(tgt_inode);
949         } else {
950                 if (dir_de) {
951                         err = -EMLINK;
952                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
953                                 goto out_dir;
954                 }
955                 ext2_inc_count(old_inode);
956                 err = ll_add_link(new_dentry, old_inode);
957                 if (err) {
958                         ext2_dec_count(old_inode);
959                         goto out_dir;
960                 }
961                 if (dir_de)
962                         ext2_inc_count(new_dir);
963         }
964
965         ext2_delete_entry (old_de, old_page);
966         ext2_dec_count(old_inode);
967
968         if (dir_de) {
969                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
970                 ext2_dec_count(old_dir);
971         }
972         return 0;
973
974 out_dir:
975         if (dir_de) {
976                 kunmap(dir_page);
977                 page_cache_release(dir_page);
978         }
979 out_old:
980         kunmap(old_page);
981         page_cache_release(old_page);
982 out:
983         return err;
984 }
985
986 struct inode_operations ll_dir_inode_operations = {
987         create:         ll_create,
988         lookup2:        ll_lookup2,
989         link:           ll_link,
990         unlink:         ll_unlink,
991         symlink:        ll_symlink,
992         mkdir:          ll_mkdir,
993         rmdir:          ll_rmdir,
994         mknod:          ll_mknod,
995         rename:         ll_rename,
996         setattr:        ll_setattr
997 };