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