Whamcloud - gitweb
b=201
[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_PW;
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_MDSINTENT, it, lock_mode,
201                          parent, dentry, &lockh, tgt, tgtlen, parent,
202                          sizeof(*parent));
203         if (rc < 0)
204                 RETURN(rc);
205         memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
206
207         request = (struct ptlrpc_request *)it->it_data;
208         if (it->it_disposition) {
209                 struct mds_body *mds_body;
210                 int mode, symlen = 0;
211                 obd_flag valid;
212
213                 /* it_disposition == 1 indicates that the server performed the
214                  * intent on our behalf.  This long block is all about fixing up
215                  * the local state so that it is correct as of the moment
216                  * _before_ the operation was applied; that way, the VFS will
217                  * think that everything is normal and call Lustre's regular
218                  * 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 the request
227                  * because we need to keep it around until ll_create gets
228                  * called.  For anything else which results in
229                  * LL_LOOKUP_POSITIVE, we can do the iget() immediately with the
230                  * contents of the reply (in the intent_finish callback).  In
231                  * the create case, however, we need to wait until
232                  * ll_create_node to do the iget() or the VFS will abort with
233                  * -EEXISTS. */
234
235                 offset = 1;
236                 mds_body = lustre_msg_buf(request->rq_repmsg, offset);
237                 ino = mds_body->fid1.id;
238                 mode = mds_body->mode;
239                 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
240                         mdc_store_create_replay_data(request, parent->i_sb);
241                         /* For create ops, we want the lookup to be negative,
242                          * unless the create failed in a way that indicates
243                          * that the file is already there */
244                         if (it->it_status != -EEXIST) {
245                                 atomic_inc(&request->rq_refcount);
246                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
247                         }
248                         /* Fall through to update attibutes. */
249                 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
250                                         IT_READLINK)) {
251                         /* For check ops, we want the lookup to succeed */
252                         it->it_data = NULL;
253                         if (it->it_status)
254                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
255                         /* Fall through to update attibutes. */
256                 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
257                         /* For rename, we want the source lookup to succeed */
258                         if (it->it_status) {
259                                 it->it_data = NULL;
260                                 GOTO(drop_req, rc = it->it_status);
261                         }
262                         it->it_data = dentry;
263                         /* Fall through to update attibutes. */
264                 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
265                         /* For remove ops, we want the lookup to succeed unless
266                          * the file truly doesn't exist */
267                         it->it_data = NULL;
268                         if (it->it_status == -ENOENT)
269                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
270                         /* No point in updating attributes that we're about to
271                          * unlink.  -phil */
272                         GOTO(out, flag = LL_LOOKUP_POSITIVE);
273                 } else if (it->it_op == IT_OPEN) {
274                         it->it_data = NULL;
275                         if (it->it_status && it->it_status != -EEXIST)
276                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
277                         /* Fall through to update attibutes. */
278                 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
279                         it->it_data = NULL;
280                         /* This means the target lookup is negative */
281                         if (mds_body->valid == 0)
282                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
283                         /* XXX bug 289: should we maybe fall through here? -p */
284                         GOTO(out, flag = LL_LOOKUP_POSITIVE);
285                 }
286
287                 /* Do a getattr now that we have the lock */
288                 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
289                 if (it->it_op == IT_READLINK) {
290                         valid |= OBD_MD_LINKNAME;
291                         symlen = mds_body->size;
292                 }
293                 ptlrpc_req_finished(request);
294                 request = NULL;
295                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
296                                  valid, symlen, &request);
297                 if (rc) {
298                         CERROR("failure %d inode "LPX64"\n", rc, ino);
299                         GOTO(drop_req, rc = -abs(rc));
300                 }
301                 offset = 0;
302         } else {
303                 struct ll_inode_info *lli = ll_i2info(parent);
304                 int mode;
305
306                 /* it_disposition == 0 indicates that it just did a simple lock
307                  * request, for which we are very thankful.  move along with
308                  * the local lookup then. */
309
310                 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
311                 offset = 0;
312
313                 ino = ll_inode_by_name(parent, dentry, &mode);
314                 if (!ino) {
315                         CERROR("inode %*s not found by name\n",
316                                dentry->d_name.len, dentry->d_name.name);
317                         GOTO(drop_lock, rc = -ENOENT);
318                 }
319
320                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
321                                  OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
322                 if (rc) {
323                         CERROR("failure %d inode "LPX64"\n", rc, ino);
324                         GOTO(drop_req, rc = -abs(rc));
325                 }
326         }
327
328         EXIT;
329  out:
330         if (intent_finish != NULL) {
331                 rc = intent_finish(flag, request, de, it, offset, ino);
332                 dentry = *de; /* intent_finish may change *de */
333         } else {
334                 ptlrpc_req_finished(request);
335         }
336
337         if (it->it_op == IT_LOOKUP || rc < 0)
338                 ll_intent_release(dentry, it);
339
340         return rc;
341
342  drop_req:
343         ptlrpc_free_req(request);
344  drop_lock:
345 #warning FIXME: must release lock here
346         return rc;
347 }
348
349 /* Search "inode"'s alias list for a dentry that has the same name and parent as
350  * de.  If found, return it.  If not found, return de. */
351 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
352 {
353         struct list_head *tmp;
354
355         spin_lock(&dcache_lock);
356         list_for_each(tmp, &inode->i_dentry) {
357                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
358
359                 /* We are called here with 'de' already on the aliases list. */
360                 if (dentry == de)
361                         continue;
362
363                 if (dentry->d_parent != de->d_parent)
364                         continue;
365
366                 if (dentry->d_name.len != de->d_name.len)
367                         continue;
368
369                 if (memcmp(dentry->d_name.name, de->d_name.name,
370                            de->d_name.len) != 0)
371                         continue;
372
373                 spin_unlock(&dcache_lock);
374                 d_rehash(dentry);
375                 return dget(dentry);
376         }
377         spin_unlock(&dcache_lock);
378         return de;
379 }
380
381 static int
382 lookup2_finish(int flag, struct ptlrpc_request *request, struct dentry **de,
383                struct lookup_intent *it, int offset, obd_id ino)
384 {
385         struct dentry *dentry = *de, *saved = *de;
386         struct inode *inode = NULL;
387         struct ll_read_inode2_cookie lic;
388
389         if (flag == LL_LOOKUP_POSITIVE) {
390                 ENTRY;
391                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
392
393                 if (S_ISREG(lic.lic_body->mode) &&
394                     lic.lic_body->valid & OBD_MD_FLEASIZE) {
395                         LASSERT(request->rq_repmsg->bufcount > offset);
396                         lic.lic_lmm = lustre_msg_buf(request->rq_repmsg,
397                                                      offset + 1);
398                 } else {
399                         lic.lic_lmm = NULL;
400                 }
401
402                 /* No rpc's happen during iget4, -ENOMEM's are possible */
403                 LASSERT(ino != 0);
404                 inode = ll_iget(dentry->d_sb, ino, &lic);
405                 if (!inode) {
406                         /* XXX make sure that request is freed in this case;
407                          * I think it is, but double-check refcounts. -phil */
408                         RETURN(-ENOMEM);
409                 }
410
411                 dentry = *de = ll_find_alias(inode, dentry);
412
413                 /* We asked for a lock on the directory, and may have been
414                  * granted a lock on the inode.  Just in case, fixup the data
415                  * pointer. */
416                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
417                                    inode, sizeof(*inode));
418
419                 EXIT;
420         } else {
421                 ENTRY;
422         }
423
424         ptlrpc_req_finished(request);
425
426         dentry->d_op = &ll_d_ops;
427         if (ll_d2d(dentry) == NULL) {
428                 ll_set_dd(dentry);
429         } else
430                 CERROR("NOT allocating fsdata - already set\n");
431
432         if (dentry == saved)
433                 d_add(dentry, inode);
434
435         if (it->it_status == 0) {
436                 LL_SAVE_INTENT(dentry, it);
437         } else {
438                 dentry->d_it = NULL;
439                 CDEBUG(D_DENTRY,
440                        "D_IT dentry %p fsdata %p intent: %s status %d\n",
441                        dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
442                        it->it_status);
443         }
444
445         RETURN(0);
446 }
447
448 static struct dentry *ll_lookup2(struct inode *parent, struct dentry *dentry,
449                                  struct lookup_intent *it)
450 {
451         struct dentry *save = dentry;
452         int rc;
453
454         rc = ll_intent_lock(parent, &dentry, it, lookup2_finish);
455         if (rc < 0) {
456                 CERROR("ll_intent_lock: %d\n", rc);
457                 return ERR_PTR(rc);
458         }
459
460         if (dentry == save)
461                 return NULL;
462         else
463                 return dentry;
464 }
465
466 static struct inode *ll_create_node(struct inode *dir, const char *name,
467                                     int namelen, const char *tgt, int tgtlen,
468                                     int mode, __u64 extra,
469                                     struct lookup_intent *it,
470                                     struct lov_stripe_md *lsm)
471 {
472         struct inode *inode;
473         struct ptlrpc_request *request = NULL;
474         struct mds_body *body;
475         time_t time = CURRENT_TIME;
476         struct ll_sb_info *sbi = ll_i2sbi(dir);
477         struct ll_read_inode2_cookie lic;
478         struct lov_mds_md *lmm = NULL;
479         ENTRY;
480
481         if (it && it->it_disposition) {
482                 int rc = it->it_status;
483                 if (rc) {
484                         CERROR("error creating MDS inode for %*s: rc = %d\n",
485                                namelen, name, rc);
486                         RETURN(ERR_PTR(rc));
487                 }
488                 ll_invalidate_inode_pages(dir);
489                 request = it->it_data;
490                 body = lustre_msg_buf(request->rq_repmsg, 1);
491                 lic.lic_lmm = NULL;
492         } else {
493                 int gid = current->fsgid;
494                 int rc;
495
496                 if (lsm) {
497                         OBD_ALLOC(lmm, lsm->lsm_mds_easize);
498                         if (!lmm)
499                                 RETURN(ERR_PTR(-ENOMEM));
500                         lov_packmd(lmm, lsm);
501                         lic.lic_lmm = lmm;
502                 } else
503                         lic.lic_lmm = NULL;
504
505                 if (dir->i_mode & S_ISGID) {
506                         gid = dir->i_gid;
507                         if (S_ISDIR(mode))
508                                 mode |= S_ISGID;
509                 }
510
511                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
512                                 tgtlen, mode, current->fsuid, gid,
513                                 time, extra, lsm, &request);
514                 if (rc) {
515                         inode = ERR_PTR(rc);
516                         GOTO(out, rc);
517                 }
518                 body = lustre_msg_buf(request->rq_repmsg, 0);
519         }
520
521         lic.lic_body = body;
522
523         LASSERT(body->ino != 0);
524         inode = ll_iget(dir->i_sb, body->ino, &lic);
525         if (IS_ERR(inode)) {
526                 int rc = PTR_ERR(inode);
527                 CERROR("new_inode -fatal: rc %d\n", rc);
528                 LBUG();
529                 GOTO(out, rc);
530         }
531
532         if (!list_empty(&inode->i_dentry)) {
533                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
534                        body->ino, atomic_read(&inode->i_count),
535                        inode->i_nlink);
536                 iput(inode);
537                 LBUG();
538                 inode = ERR_PTR(-EIO);
539                 GOTO(out, -EIO);
540         }
541
542         if (it && it->it_disposition) {
543                 /* We asked for a lock on the directory, but were
544                  * granted a lock on the inode.  Since we finally have
545                  * an inode pointer, stuff it in the lock. */
546                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
547                                    inode, sizeof(*inode));
548         }
549
550         EXIT;
551  out:
552         if (lsm && lmm)
553                 OBD_FREE(lmm, lsm->lsm_mds_easize);
554         ptlrpc_req_finished(request);
555         return inode;
556 }
557
558 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
559                          const char *name, int len)
560 {
561         struct ptlrpc_request *request = NULL;
562         struct ll_sb_info *sbi = ll_i2sbi(dir);
563         int err;
564
565         ENTRY;
566
567         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
568                          &request);
569         ptlrpc_req_finished(request);
570
571         RETURN(err);
572 }
573
574 int ll_mdc_link(struct dentry *src, struct inode *dir,
575                 const char *name, int len)
576 {
577         struct ptlrpc_request *request = NULL;
578         int err;
579         struct ll_sb_info *sbi = ll_i2sbi(dir);
580
581         ENTRY;
582
583         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
584         ptlrpc_req_finished(request);
585
586         RETURN(err);
587 }
588
589 int ll_mdc_rename(struct inode *src, struct inode *tgt,
590                   struct dentry *old, struct dentry *new)
591 {
592         struct ptlrpc_request *request = NULL;
593         struct ll_sb_info *sbi = ll_i2sbi(src);
594         int err;
595
596         ENTRY;
597
598         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
599                          old->d_name.name, old->d_name.len,
600                          new->d_name.name, new->d_name.len, &request);
601         ptlrpc_req_finished(request);
602
603         RETURN(err);
604 }
605
606 /*
607  * By the time this is called, we already have created the directory cache
608  * entry for the new file, but it is so far negative - it has no inode.
609  * We defer creating the OBD object(s) until open, to keep the intent and
610  * non-intent code paths similar, and also because we do not have the MDS
611  * inode number before calling ll_create_node() (which is needed for LOV),
612  * so we would need to do yet another RPC to the MDS to store the LOV EA
613  * data on the MDS.
614  *
615  * If the create succeeds, we fill in the inode information
616  * with d_instantiate().
617  */
618 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
619 {
620         struct lookup_intent *it;
621         struct inode *inode;
622         int rc = 0;
623         ENTRY;
624
625         LL_GET_INTENT(dentry, it);
626
627         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
628                                NULL, 0, mode, 0, it, NULL);
629
630         if (IS_ERR(inode))
631                 RETURN(PTR_ERR(inode));
632
633         if (it->it_disposition) {
634                 struct ll_inode_info *lli = ll_i2info(inode);
635                 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
636                        sizeof(lli->lli_intent_lock_handle));
637                 d_instantiate(dentry, inode);
638         } else {
639                 /* no directory data updates when intents rule */
640                 rc = ext2_add_nondir(dentry, inode);
641         }
642
643         RETURN(rc);
644 }
645
646 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
647                     int rdev)
648 {
649         struct lookup_intent *it;
650         struct inode *inode;
651         int rc = 0;
652
653         LL_GET_INTENT(dentry, it);
654
655         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
656                                NULL, 0, mode, rdev, it, NULL);
657
658         if (IS_ERR(inode))
659                 RETURN(PTR_ERR(inode));
660
661         /* no directory data updates when intents rule */
662         if (it && it->it_disposition)
663                 d_instantiate(dentry, inode);
664         else
665                 rc = ext2_add_nondir(dentry, inode);
666
667         return rc;
668 }
669
670 static int ll_symlink(struct inode *dir, struct dentry *dentry,
671                       const char *symname)
672 {
673         struct lookup_intent *it;
674         unsigned l = strlen(symname);
675         struct inode *inode;
676         struct ll_inode_info *lli;
677         int err = 0;
678         ENTRY;
679
680         LL_GET_INTENT(dentry, it);
681
682         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
683                                symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
684         if (IS_ERR(inode))
685                 RETURN(PTR_ERR(inode));
686
687         lli = ll_i2info(inode);
688
689         OBD_ALLOC(lli->lli_symlink_name, l + 1);
690         /* this _could_ be a non-fatal error, since the symlink is already
691          * stored on the MDS by this point, and we can re-get it in readlink.
692          */
693         if (!lli->lli_symlink_name)
694                 RETURN(-ENOMEM);
695
696         memcpy(lli->lli_symlink_name, symname, l + 1);
697         inode->i_size = l;
698
699         /* no directory data updates when intents rule */
700         if (it && it->it_disposition)
701                 d_instantiate(dentry, inode);
702         else
703                 err = ext2_add_nondir(dentry, inode);
704
705         RETURN(err);
706 }
707
708 static int ll_link(struct dentry *old_dentry, struct inode * dir,
709                    struct dentry *dentry)
710 {
711         struct lookup_intent *it;
712         struct inode *inode = old_dentry->d_inode;
713         int rc;
714
715         LL_GET_INTENT(dentry, it);
716
717         if (it && it->it_disposition) {
718                 if (it->it_status)
719                         RETURN(it->it_status);
720                 inode->i_ctime = CURRENT_TIME;
721                 ext2_inc_count(inode);
722                 atomic_inc(&inode->i_count);
723                 d_instantiate(dentry, inode);
724                 ll_invalidate_inode_pages(dir);
725                 RETURN(0);
726         }
727
728         if (S_ISDIR(inode->i_mode))
729                 return -EPERM;
730
731         if (inode->i_nlink >= EXT2_LINK_MAX)
732                 return -EMLINK;
733
734         rc = ll_mdc_link(old_dentry, dir,
735                           dentry->d_name.name, dentry->d_name.len);
736         if (rc)
737                 RETURN(rc);
738
739         inode->i_ctime = CURRENT_TIME;
740         ext2_inc_count(inode);
741         atomic_inc(&inode->i_count);
742
743         return ext2_add_nondir(dentry, inode);
744 }
745
746 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
747 {
748         struct lookup_intent *it;
749         struct inode * inode;
750         int err = -EMLINK;
751         ENTRY;
752
753         LL_GET_INTENT(dentry, it);
754
755         if (dir->i_nlink >= EXT2_LINK_MAX)
756                 goto out;
757
758         ext2_inc_count(dir);
759         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
760                                NULL, 0, S_IFDIR | mode, 0, it, NULL);
761         err = PTR_ERR(inode);
762         if (IS_ERR(inode))
763                 goto out_dir;
764
765         ext2_inc_count(inode);
766
767         err = ext2_make_empty(inode, dir);
768         if (err)
769                 goto out_fail;
770
771         /* no directory data updates when intents rule */
772         if (!it || !it->it_disposition) {
773                 err = ll_add_link(dentry, inode);
774                 if (err)
775                         goto out_fail;
776         }
777
778         d_instantiate(dentry, inode);
779 out:
780         EXIT;
781         return err;
782
783 out_fail:
784         ext2_dec_count(inode);
785         ext2_dec_count(inode);
786         iput(inode);
787         EXIT;
788 out_dir:
789         ext2_dec_count(dir);
790         EXIT;
791         goto out;
792 }
793
794 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
795                             struct lookup_intent *it, __u32 mode)
796 {
797         struct inode *inode = dentry->d_inode;
798         struct ext2_dir_entry_2 * de;
799         struct page * page;
800         int rc = 0;
801
802         if (it && it->it_disposition) {
803                 rc = it->it_status;
804                 ll_invalidate_inode_pages(dir);
805                 if (rc)
806                         GOTO(out, rc);
807                 GOTO(out_dec, 0);
808         }
809
810         de = ext2_find_entry(dir, dentry, &page);
811         if (!de)
812                 GOTO(out, rc = -ENOENT);
813         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
814                            dentry->d_name.name, dentry->d_name.len);
815         if (rc)
816                 GOTO(out, rc);
817
818         rc = ext2_delete_entry(de, page);
819         if (rc)
820                 GOTO(out, rc);
821
822         /* AED: not sure if needed - directory lock revocation should do it
823          * in the case where the client has cached it for non-intent ops.
824          */
825         ll_invalidate_inode_pages(dir);
826
827         inode->i_ctime = dir->i_ctime;
828 out_dec:
829         ext2_dec_count(inode);
830 out:
831         return rc;
832 }
833
834 static int ll_unlink(struct inode *dir, struct dentry *dentry)
835 {
836         struct lookup_intent * it;
837
838         LL_GET_INTENT(dentry, it);
839
840         return ll_common_unlink(dir, dentry, it, S_IFREG);
841 }
842
843 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
844 {
845         struct inode * inode = dentry->d_inode;
846         struct lookup_intent *it;
847         int rc;
848         ENTRY;
849
850         LL_GET_INTENT(dentry, it);
851
852         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
853                 RETURN(-ENOTEMPTY);
854
855         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
856         if (!rc) {
857                 inode->i_size = 0;
858                 ext2_dec_count(inode);
859                 ext2_dec_count(dir);
860         }
861
862         RETURN(rc);
863 }
864
865 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
866                      struct inode * new_dir, struct dentry * new_dentry)
867 {
868         struct lookup_intent *it;
869         struct inode * old_inode = old_dentry->d_inode;
870         struct inode * tgt_inode = new_dentry->d_inode;
871         struct page * dir_page = NULL;
872         struct ext2_dir_entry_2 * dir_de = NULL;
873         struct ext2_dir_entry_2 * old_de;
874         struct page * old_page;
875         int err;
876
877         LL_GET_INTENT(new_dentry, it);
878
879         if (it && it->it_disposition) {
880                 if (tgt_inode) {
881                         tgt_inode->i_ctime = CURRENT_TIME;
882                         tgt_inode->i_nlink--;
883                 }
884                 ll_invalidate_inode_pages(old_dir);
885                 ll_invalidate_inode_pages(new_dir);
886                 GOTO(out, err = it->it_status);
887         }
888
889         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
890         if (err)
891                 goto out;
892
893         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
894         if (!old_de)
895                 goto out;
896
897         if (S_ISDIR(old_inode->i_mode)) {
898                 err = -EIO;
899                 dir_de = ext2_dotdot(old_inode, &dir_page);
900                 if (!dir_de)
901                         goto out_old;
902         }
903
904         if (tgt_inode) {
905                 struct page *new_page;
906                 struct ext2_dir_entry_2 *new_de;
907
908                 err = -ENOTEMPTY;
909                 if (dir_de && !ext2_empty_dir (tgt_inode))
910                         goto out_dir;
911
912                 err = -ENOENT;
913                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
914                 if (!new_de)
915                         goto out_dir;
916                 ext2_inc_count(old_inode);
917                 ext2_set_link(new_dir, new_de, new_page, old_inode);
918                 tgt_inode->i_ctime = CURRENT_TIME;
919                 if (dir_de)
920                         tgt_inode->i_nlink--;
921                 ext2_dec_count(tgt_inode);
922         } else {
923                 if (dir_de) {
924                         err = -EMLINK;
925                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
926                                 goto out_dir;
927                 }
928                 ext2_inc_count(old_inode);
929                 err = ll_add_link(new_dentry, old_inode);
930                 if (err) {
931                         ext2_dec_count(old_inode);
932                         goto out_dir;
933                 }
934                 if (dir_de)
935                         ext2_inc_count(new_dir);
936         }
937
938         ext2_delete_entry (old_de, old_page);
939         ext2_dec_count(old_inode);
940
941         if (dir_de) {
942                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
943                 ext2_dec_count(old_dir);
944         }
945         return 0;
946
947 out_dir:
948         if (dir_de) {
949                 kunmap(dir_page);
950                 page_cache_release(dir_page);
951         }
952 out_old:
953         kunmap(old_page);
954         page_cache_release(old_page);
955 out:
956         return err;
957 }
958
959 struct inode_operations ll_dir_inode_operations = {
960         create:         ll_create,
961         lookup2:        ll_lookup2,
962         link:           ll_link,
963         unlink:         ll_unlink,
964         symlink:        ll_symlink,
965         mkdir:          ll_mkdir,
966         rmdir:          ll_rmdir,
967         mknod:          ll_mknod,
968         rename:         ll_rename,
969         setattr:        ll_setattr
970 };