Whamcloud - gitweb
- Beginning of new REINT replay infrastructure.
[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_lock(struct inode *dir, struct dentry *dentry,
113             struct lookup_intent *it, struct lustre_handle *lockh)
114 {
115         struct ll_sb_info *sbi = ll_i2sbi(dir);
116         char *tgt = NULL;
117         int tgtlen = 0;
118         int err, lock_mode;
119
120         /* CREAT needs to be tested before open (both could be set) */
121         if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD))) {
122                 lock_mode = LCK_PW;
123         } else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
124                                 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
125                                 IT_LINK | IT_LINK2 | IT_LOOKUP)) {
126                 /* XXXphil PW for LINK2/RENAME2? */
127                 lock_mode = LCK_PR;
128         } else if (it->it_op & IT_SYMLINK) {
129                 lock_mode = LCK_PW;
130                 tgt = it->it_data;
131                 tgtlen = strlen(tgt);
132                 it->it_data = NULL;
133         } else {
134                 LBUG();
135                 RETURN(-EINVAL);
136         }
137
138         err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode,
139                           dir, dentry, lockh, tgt, tgtlen, dir, sizeof(*dir));
140
141         RETURN(err);
142 }
143
144 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
145 {
146         ENTRY;
147
148         ldlm_lock_decref(lockh, mode);
149
150         RETURN(0);
151 }
152
153 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
154 extern int ll_read_inode2(struct inode *inode, void *opaque);
155 struct inode *ll_iget(struct super_block *sb, ino_t hash,
156                       struct ll_read_inode2_cookie *lic)
157 {
158         struct inode *inode;
159
160         inode = iget5_locked(sb, hash, ll_test_inode, ll_read_inode2, lic);
161
162         if (!inode)
163                 return ERR_PTR(-ENOMEM);
164
165         if (inode->i_state & I_NEW) {
166
167                 unlock_new_inode(inode);
168         }
169
170         // XXX Coda always fills inodes, should Lustre?
171         return inode;
172 }
173 #else
174 struct inode *ll_iget(struct super_block *sb, ino_t hash,
175                       struct ll_read_inode2_cookie *lic)
176 {
177         struct inode *inode;
178         inode = iget4(sb, hash, ll_find_inode, lic);
179         return inode;
180 }
181 #endif
182
183 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
184                                  struct lookup_intent *it)
185 {
186         struct ptlrpc_request *request = NULL;
187         struct inode * inode = NULL;
188         struct ll_sb_info *sbi = ll_i2sbi(dir);
189         struct ll_read_inode2_cookie lic;
190         struct lustre_handle lockh;
191         struct lookup_intent lookup_it = { IT_LOOKUP };
192         int rc, offset;
193         obd_id ino = 0;
194
195         ENTRY;
196
197         if (it == NULL)
198                 it = &lookup_it;
199
200         CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
201                dentry->d_name.name, ldlm_it2str(it->it_op));
202
203         if (dentry->d_name.len > EXT2_NAME_LEN)
204                 RETURN(ERR_PTR(-ENAMETOOLONG));
205
206         rc = ll_lock(dir, dentry, it, &lockh);
207         if (rc < 0)
208                 RETURN(ERR_PTR(rc));
209         memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
210
211         request = (struct ptlrpc_request *)it->it_data;
212         if (it->it_disposition) {
213                 int mode, symlen = 0;
214                 obd_flag valid;
215
216                 offset = 1;
217                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
218                 ino = lic.lic_body->fid1.id;
219                 mode = lic.lic_body->mode;
220                 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
221                         /* For create ops, we want the lookup to be negative,
222                          * unless the create failed in a way that indicates
223                          * that the file is already there */
224                         if (it->it_status != -EEXIST)
225                                 GOTO(negative, NULL);
226                 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
227                                         IT_READLINK)) {
228                         /* For check ops, we want the lookup to succeed */
229                         it->it_data = NULL;
230                         if (it->it_status)
231                                 GOTO(neg_req, NULL);
232                 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
233                         /* For rename, we want the source lookup to succeed */
234                         if (it->it_status) {
235                                 it->it_data = NULL;
236                                 GOTO(drop_req, rc = it->it_status);
237                         }
238                         it->it_data = dentry;
239                 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
240                         /* For remove ops, we want the lookup to succeed unless
241                          * the file truly doesn't exist */
242                         it->it_data = NULL;
243                         if (it->it_status == -ENOENT)
244                                 GOTO(neg_req, NULL);
245                         goto iget;
246                 } else if (it->it_op == IT_OPEN) {
247                         it->it_data = NULL;
248                         if (it->it_status && it->it_status != -EEXIST)
249                                 GOTO(neg_req, NULL);
250                 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
251                         struct mds_body *body =
252                                 lustre_msg_buf(request->rq_repmsg, offset);
253                         it->it_data = NULL;
254                         /* This means the target lookup is negative */
255                         if (body->valid == 0)
256                                 GOTO(neg_req, NULL);
257                         goto iget;
258                 }
259
260                 /* Do a getattr now that we have the lock */
261                 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
262                 if (it->it_op == IT_READLINK) {
263                         valid |= OBD_MD_LINKNAME;
264                         symlen = lic.lic_body->size;
265                 }
266                 ptlrpc_req_finished(request);
267                 request = NULL;
268                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
269                                  valid, symlen, &request);
270                 if (rc) {
271                         CERROR("failure %d inode "LPX64"\n", rc, ino);
272                         GOTO(drop_req, rc = -abs(rc));
273                 }
274                 offset = 0;
275         } else {
276                 struct ll_inode_info *lli = ll_i2info(dir);
277                 int mode;
278
279                 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
280                 offset = 0;
281
282                 ino = ll_inode_by_name(dir, dentry, &mode);
283                 if (!ino) {
284                         CERROR("inode %*s not found by name\n",
285                                dentry->d_name.len, dentry->d_name.name);
286                         GOTO(drop_lock, rc = -ENOENT);
287                 }
288
289                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
290                                  OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
291                 if (rc) {
292                         CERROR("failure %d inode "LPX64"\n", rc, ino);
293                         GOTO(drop_req, rc = -abs(rc));
294                 }
295         }
296
297  iget:
298         lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
299         if (S_ISREG(lic.lic_body->mode) &&
300             lic.lic_body->valid & OBD_MD_FLEASIZE) {
301                 LASSERT(request->rq_repmsg->bufcount > offset);
302                 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
303         } else
304                 lic.lic_lmm = NULL;
305
306         /* No rpc's happen during iget4, -ENOMEM's are possible */
307         LASSERT(ino != 0);
308         inode = ll_iget(dir->i_sb, ino, &lic);
309         if (!inode) {
310                 ptlrpc_free_req(request);
311                 ll_intent_release(dentry, it);
312                 RETURN(ERR_PTR(-ENOMEM));
313         }
314
315         EXIT;
316  neg_req:
317         ptlrpc_req_finished(request);
318  negative:
319         dentry->d_op = &ll_d_ops;
320         if (ll_d2d(dentry) == NULL) {
321                 ll_set_dd(dentry);
322         } else
323                 CERROR("NOT allocating fsdata - already set\n");
324
325         d_add(dentry, inode);
326
327         if (it->it_status == 0) {
328                 LL_SAVE_INTENT(dentry, it);
329         } else {
330                 dentry->d_it = NULL;
331                 CDEBUG(D_DENTRY,
332                        "D_IT dentry %p fsdata %p intent: %s status %d\n",
333                        dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
334                        it->it_status);
335         }
336
337         if (it->it_op == IT_LOOKUP)
338                 ll_intent_release(dentry, it);
339
340         return NULL;
341
342  drop_req:
343         ptlrpc_free_req(request);
344  drop_lock:
345 #warning FIXME: must release lock here
346         return ERR_PTR(rc);
347 }
348
349 static struct inode *ll_create_node(struct inode *dir, const char *name,
350                                     int namelen, const char *tgt, int tgtlen,
351                                     int mode, __u64 extra,
352                                     struct lookup_intent *it,
353                                     struct lov_stripe_md *lsm)
354 {
355         struct inode *inode;
356         struct ptlrpc_request *request = NULL;
357         struct mds_body *body;
358         time_t time = CURRENT_TIME;
359         struct ll_sb_info *sbi = ll_i2sbi(dir);
360         struct ll_read_inode2_cookie lic;
361         struct lov_mds_md *lmm = NULL;
362         ENTRY;
363
364         if (it && it->it_disposition) {
365                 int rc = it->it_status;
366                 if (rc) {
367                         CERROR("error creating MDS inode for %*s: rc = %d\n",
368                                namelen, name, rc);
369                         RETURN(ERR_PTR(rc));
370                 }
371                 ll_invalidate_inode_pages(dir);
372                 request = it->it_data;
373                 body = lustre_msg_buf(request->rq_repmsg, 1);
374                 lic.lic_lmm = NULL;
375         } else {
376                 int gid = current->fsgid;
377                 int rc;
378
379                 if (lsm) {
380                         OBD_ALLOC(lmm, lsm->lsm_mds_easize);
381                         if (!lmm)
382                                 RETURN(ERR_PTR(-ENOMEM));
383                         lov_packmd(lmm, lsm);
384                         lic.lic_lmm = lmm;
385                 } else
386                         lic.lic_lmm = NULL;
387
388                 if (dir->i_mode & S_ISGID) {
389                         gid = dir->i_gid;
390                         if (S_ISDIR(mode))
391                                 mode |= S_ISGID;
392                 }
393
394                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
395                                 tgtlen, mode, current->fsuid, gid,
396                                 time, extra, lsm, &request);
397                 if (rc) {
398                         inode = ERR_PTR(rc);
399                         GOTO(out, rc);
400                 }
401                 body = lustre_msg_buf(request->rq_repmsg, 0);
402         }
403
404         lic.lic_body = body;
405
406         LASSERT(body->ino != 0);
407         inode = ll_iget(dir->i_sb, body->ino, &lic);
408         if (IS_ERR(inode)) {
409                 int rc = PTR_ERR(inode);
410                 CERROR("new_inode -fatal: rc %d\n", rc);
411                 LBUG();
412                 GOTO(out, rc);
413         }
414
415         if (!list_empty(&inode->i_dentry)) {
416                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
417                        body->ino, atomic_read(&inode->i_count),
418                        inode->i_nlink);
419                 iput(inode);
420                 LBUG();
421                 inode = ERR_PTR(-EIO);
422                 GOTO(out, -EIO);
423         }
424
425         EXIT;
426  out:
427         if (lsm && lmm)
428                 OBD_FREE(lmm, lsm->lsm_mds_easize);
429         ptlrpc_req_finished(request);
430         return inode;
431 }
432
433 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
434                          const char *name, int len)
435 {
436         struct ptlrpc_request *request = NULL;
437         struct ll_sb_info *sbi = ll_i2sbi(dir);
438         int err;
439
440         ENTRY;
441
442         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
443                          &request);
444         ptlrpc_req_finished(request);
445
446         RETURN(err);
447 }
448
449 int ll_mdc_link(struct dentry *src, struct inode *dir,
450                 const char *name, int len)
451 {
452         struct ptlrpc_request *request = NULL;
453         int err;
454         struct ll_sb_info *sbi = ll_i2sbi(dir);
455
456         ENTRY;
457
458         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
459         ptlrpc_req_finished(request);
460
461         RETURN(err);
462 }
463
464 int ll_mdc_rename(struct inode *src, struct inode *tgt,
465                   struct dentry *old, struct dentry *new)
466 {
467         struct ptlrpc_request *request = NULL;
468         struct ll_sb_info *sbi = ll_i2sbi(src);
469         int err;
470
471         ENTRY;
472
473         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
474                          old->d_name.name, old->d_name.len,
475                          new->d_name.name, new->d_name.len, &request);
476         ptlrpc_req_finished(request);
477
478         RETURN(err);
479 }
480
481 /*
482  * By the time this is called, we already have created the directory cache
483  * entry for the new file, but it is so far negative - it has no inode.
484  * We defer creating the OBD object(s) until open, to keep the intent and
485  * non-intent code paths similar, and also because we do not have the MDS
486  * inode number before calling ll_create_node() (which is needed for LOV),
487  * so we would need to do yet another RPC to the MDS to store the LOV EA
488  * data on the MDS.
489  *
490  * If the create succeeds, we fill in the inode information
491  * with d_instantiate().
492  */
493 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
494 {
495         struct lookup_intent *it;
496         struct inode *inode;
497         int rc = 0;
498         ENTRY;
499
500         LL_GET_INTENT(dentry, it);
501
502         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
503                                NULL, 0, mode, 0, it, NULL);
504
505         if (IS_ERR(inode))
506                 RETURN(PTR_ERR(inode));
507
508         if (it->it_disposition) {
509                 struct ll_inode_info *lli = ll_i2info(inode);
510                 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
511                        sizeof(lli->lli_intent_lock_handle));
512                 d_instantiate(dentry, inode);
513         } else {
514                 /* no directory data updates when intents rule */
515                 rc = ext2_add_nondir(dentry, inode);
516         }
517
518         RETURN(rc);
519 }
520
521 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
522                     int rdev)
523 {
524         struct lookup_intent *it;
525         struct inode *inode;
526         int rc = 0;
527
528         LL_GET_INTENT(dentry, it);
529
530         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
531                                NULL, 0, mode, rdev, it, NULL);
532
533         if (IS_ERR(inode))
534                 RETURN(PTR_ERR(inode));
535
536         /* no directory data updates when intents rule */
537         if (it && it->it_disposition)
538                 d_instantiate(dentry, inode);
539         else
540                 rc = ext2_add_nondir(dentry, inode);
541
542         return rc;
543 }
544
545 static int ll_symlink(struct inode *dir, struct dentry *dentry,
546                       const char *symname)
547 {
548         struct lookup_intent *it;
549         unsigned l = strlen(symname);
550         struct inode *inode;
551         struct ll_inode_info *lli;
552         int err = 0;
553         ENTRY;
554
555         LL_GET_INTENT(dentry, it);
556
557         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
558                                symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
559         if (IS_ERR(inode))
560                 RETURN(PTR_ERR(inode));
561
562         lli = ll_i2info(inode);
563
564         OBD_ALLOC(lli->lli_symlink_name, l + 1);
565         /* this _could_ be a non-fatal error, since the symlink is already
566          * stored on the MDS by this point, and we can re-get it in readlink.
567          */
568         if (!lli->lli_symlink_name)
569                 RETURN(-ENOMEM);
570
571         memcpy(lli->lli_symlink_name, symname, l + 1);
572         inode->i_size = l;
573
574         /* no directory data updates when intents rule */
575         if (it && it->it_disposition)
576                 d_instantiate(dentry, inode);
577         else
578                 err = ext2_add_nondir(dentry, inode);
579
580         RETURN(err);
581 }
582
583 static int ll_link(struct dentry *old_dentry, struct inode * dir,
584                    struct dentry *dentry)
585 {
586         struct lookup_intent *it;
587         struct inode *inode = old_dentry->d_inode;
588         int rc;
589
590         LL_GET_INTENT(dentry, it);
591
592         if (it && it->it_disposition) {
593                 if (it->it_status)
594                         RETURN(it->it_status);
595                 inode->i_ctime = CURRENT_TIME;
596                 ext2_inc_count(inode);
597                 atomic_inc(&inode->i_count);
598                 d_instantiate(dentry, inode);
599                 ll_invalidate_inode_pages(dir);
600                 RETURN(0);
601         }
602
603         if (S_ISDIR(inode->i_mode))
604                 return -EPERM;
605
606         if (inode->i_nlink >= EXT2_LINK_MAX)
607                 return -EMLINK;
608
609         rc = ll_mdc_link(old_dentry, dir,
610                           dentry->d_name.name, dentry->d_name.len);
611         if (rc)
612                 RETURN(rc);
613
614         inode->i_ctime = CURRENT_TIME;
615         ext2_inc_count(inode);
616         atomic_inc(&inode->i_count);
617
618         return ext2_add_nondir(dentry, inode);
619 }
620
621 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
622 {
623         struct lookup_intent *it;
624         struct inode * inode;
625         int err = -EMLINK;
626         ENTRY;
627
628         LL_GET_INTENT(dentry, it);
629
630         if (dir->i_nlink >= EXT2_LINK_MAX)
631                 goto out;
632
633         ext2_inc_count(dir);
634         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
635                                NULL, 0, S_IFDIR | mode, 0, it, NULL);
636         err = PTR_ERR(inode);
637         if (IS_ERR(inode))
638                 goto out_dir;
639
640         ext2_inc_count(inode);
641
642         err = ext2_make_empty(inode, dir);
643         if (err)
644                 goto out_fail;
645
646         /* no directory data updates when intents rule */
647         if (!it || !it->it_disposition) {
648                 err = ll_add_link(dentry, inode);
649                 if (err)
650                         goto out_fail;
651         }
652
653         d_instantiate(dentry, inode);
654 out:
655         EXIT;
656         return err;
657
658 out_fail:
659         ext2_dec_count(inode);
660         ext2_dec_count(inode);
661         iput(inode);
662         EXIT;
663 out_dir:
664         ext2_dec_count(dir);
665         EXIT;
666         goto out;
667 }
668
669 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
670                             struct lookup_intent *it, __u32 mode)
671 {
672         struct inode *inode = dentry->d_inode;
673         struct ext2_dir_entry_2 * de;
674         struct page * page;
675         int rc = 0;
676
677         if (it && it->it_disposition) {
678                 rc = it->it_status;
679                 ll_invalidate_inode_pages(dir);
680                 if (rc)
681                         GOTO(out, rc);
682                 GOTO(out_dec, 0);
683         }
684
685         de = ext2_find_entry(dir, dentry, &page);
686         if (!de)
687                 GOTO(out, rc = -ENOENT);
688         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
689                            dentry->d_name.name, dentry->d_name.len);
690         if (rc)
691                 GOTO(out, rc);
692
693         rc = ext2_delete_entry(de, page);
694         if (rc)
695                 GOTO(out, rc);
696
697         /* AED: not sure if needed - directory lock revocation should do it
698          * in the case where the client has cached it for non-intent ops.
699          */
700         ll_invalidate_inode_pages(dir);
701
702         inode->i_ctime = dir->i_ctime;
703 out_dec:
704         ext2_dec_count(inode);
705 out:
706         return rc;
707 }
708
709 static int ll_unlink(struct inode *dir, struct dentry *dentry)
710 {
711         struct lookup_intent * it;
712
713         LL_GET_INTENT(dentry, it);
714
715         return ll_common_unlink(dir, dentry, it, S_IFREG);
716 }
717
718 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
719 {
720         struct inode * inode = dentry->d_inode;
721         struct lookup_intent *it;
722         int rc;
723         ENTRY;
724
725         LL_GET_INTENT(dentry, it);
726
727         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
728                 RETURN(-ENOTEMPTY);
729
730         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
731         if (!rc) {
732                 inode->i_size = 0;
733                 ext2_dec_count(inode);
734                 ext2_dec_count(dir);
735         }
736
737         RETURN(rc);
738 }
739
740 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
741                      struct inode * new_dir, struct dentry * new_dentry)
742 {
743         struct lookup_intent *it;
744         struct inode * old_inode = old_dentry->d_inode;
745         struct inode * tgt_inode = new_dentry->d_inode;
746         struct page * dir_page = NULL;
747         struct ext2_dir_entry_2 * dir_de = NULL;
748         struct ext2_dir_entry_2 * old_de;
749         struct page * old_page;
750         int err;
751
752         LL_GET_INTENT(new_dentry, it);
753
754         if (it && it->it_disposition) {
755                 if (tgt_inode) {
756                         tgt_inode->i_ctime = CURRENT_TIME;
757                         tgt_inode->i_nlink--;
758                 }
759                 ll_invalidate_inode_pages(old_dir);
760                 ll_invalidate_inode_pages(new_dir);
761                 GOTO(out, err = it->it_status);
762         }
763
764         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
765         if (err)
766                 goto out;
767
768         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
769         if (!old_de)
770                 goto out;
771
772         if (S_ISDIR(old_inode->i_mode)) {
773                 err = -EIO;
774                 dir_de = ext2_dotdot(old_inode, &dir_page);
775                 if (!dir_de)
776                         goto out_old;
777         }
778
779         if (tgt_inode) {
780                 struct page *new_page;
781                 struct ext2_dir_entry_2 *new_de;
782
783                 err = -ENOTEMPTY;
784                 if (dir_de && !ext2_empty_dir (tgt_inode))
785                         goto out_dir;
786
787                 err = -ENOENT;
788                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
789                 if (!new_de)
790                         goto out_dir;
791                 ext2_inc_count(old_inode);
792                 ext2_set_link(new_dir, new_de, new_page, old_inode);
793                 tgt_inode->i_ctime = CURRENT_TIME;
794                 if (dir_de)
795                         tgt_inode->i_nlink--;
796                 ext2_dec_count(tgt_inode);
797         } else {
798                 if (dir_de) {
799                         err = -EMLINK;
800                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
801                                 goto out_dir;
802                 }
803                 ext2_inc_count(old_inode);
804                 err = ll_add_link(new_dentry, old_inode);
805                 if (err) {
806                         ext2_dec_count(old_inode);
807                         goto out_dir;
808                 }
809                 if (dir_de)
810                         ext2_inc_count(new_dir);
811         }
812
813         ext2_delete_entry (old_de, old_page);
814         ext2_dec_count(old_inode);
815
816         if (dir_de) {
817                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
818                 ext2_dec_count(old_dir);
819         }
820         return 0;
821
822 out_dir:
823         if (dir_de) {
824                 kunmap(dir_page);
825                 page_cache_release(dir_page);
826         }
827 out_old:
828         kunmap(old_page);
829         page_cache_release(old_page);
830 out:
831         return err;
832 }
833
834 struct inode_operations ll_dir_inode_operations = {
835         create:         ll_create,
836         lookup2:        ll_lookup2,
837         link:           ll_link,
838         unlink:         ll_unlink,
839         symlink:        ll_symlink,
840         mkdir:          ll_mkdir,
841         rmdir:          ll_rmdir,
842         mknod:          ll_mknod,
843         rename:         ll_rename,
844         setattr:        ll_setattr
845 };