Whamcloud - gitweb
b948665d68b03853d5f523dc2a1ddb729e2eca54
[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/locks.h>
32 #include <linux/quotaops.h>
33
34 #define DEBUG_SUBSYSTEM S_LLITE
35
36 #include <linux/obd_support.h>
37 #include <linux/lustre_lite.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/obd_lov.h>
40
41 extern struct address_space_operations ll_aops;
42
43 /* from super.c */
44 extern void ll_change_inode(struct inode *inode);
45 extern int ll_setattr(struct dentry *de, struct iattr *attr);
46
47 /* from dir.c */
48 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
49 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
50 int ext2_make_empty(struct inode *inode, struct inode *parent);
51 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
52                    struct dentry *dentry, struct page ** res_page);
53 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
54 int ext2_empty_dir (struct inode * inode);
55 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
56 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
57                    struct page *page, struct inode *inode);
58
59 /*
60  * Couple of helper functions - make the code slightly cleaner.
61  */
62 static inline void ext2_inc_count(struct inode *inode)
63 {
64         inode->i_nlink++;
65 }
66
67 /* postpone the disk update until the inode really goes away */
68 static inline void ext2_dec_count(struct inode *inode)
69 {
70         inode->i_nlink--;
71 }
72
73 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
74 {
75         int err;
76         err = ll_add_link(dentry, inode);
77         if (!err) {
78                 d_instantiate(dentry, inode);
79                 return 0;
80         }
81         ext2_dec_count(inode);
82         iput(inode);
83         return err;
84 }
85
86 /* methods */
87 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
88 {
89         struct ll_read_inode2_cookie *lic = opaque;
90         struct mds_body *body = lic->lic_body;
91
92         if (inode->i_generation != lic->lic_body->generation)
93                 return 0;
94
95         /* Apply the attributes in 'opaque' to this inode */
96         ll_update_inode(inode, body);
97
98         return 1;
99 }
100
101 extern struct dentry_operations ll_d_ops;
102
103 int ll_lock(struct inode *dir, struct dentry *dentry,
104             struct lookup_intent *it, struct lustre_handle *lockh)
105 {
106         struct ll_sb_info *sbi = ll_i2sbi(dir);
107         char *tgt = NULL;
108         int tgtlen = 0;
109         int err, lock_mode;
110
111         if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD)))
112                 lock_mode = LCK_PW;
113         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
114                               IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
115                               IT_LINK | IT_LINK2))
116                 lock_mode = LCK_PR;
117         else if (it->it_op & IT_SYMLINK) {
118                 lock_mode = LCK_PW;
119                 tgt = it->it_data;
120                 tgtlen = strlen(tgt);
121                 it->it_data = NULL;
122         } else if (it->it_op & IT_LOOKUP)
123                 lock_mode = LCK_PR;
124         else {
125                 LBUG();
126                 RETURN(-EINVAL);
127         }
128
129         err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode,
130                           dir, dentry, lockh, tgt, tgtlen, dir, sizeof(*dir));
131
132         RETURN(err);
133 }
134
135 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
136 {
137         ENTRY;
138
139         ldlm_lock_decref(lockh, mode);
140
141         RETURN(0);
142 }
143
144 static struct dentry *ll_lookup2(struct inode *dir, struct dentry *dentry,
145                                  struct lookup_intent *it)
146 {
147         struct ptlrpc_request *request = NULL;
148         struct inode * inode = NULL;
149         struct ll_sb_info *sbi = ll_i2sbi(dir);
150         struct ll_read_inode2_cookie lic;
151         struct lustre_handle lockh;
152         struct lookup_intent lookup_it = { IT_LOOKUP };
153         int err, offset;
154         obd_id ino = 0;
155
156         ENTRY;
157
158         /* CHECK_MOUNT_EPOCH(dir); */
159         if (ll_i2info(dir)->lli_mount_epoch != ll_i2sbi(dir)->ll_mount_epoch)
160                 RETURN(ERR_PTR(-EIO));
161
162         if (it == NULL) {
163                 it = &lookup_it;
164                 dentry->d_it = it;
165         }
166
167         CDEBUG(D_INFO, "name: %*s, intent op: %d\n", dentry->d_name.len,
168                dentry->d_name.name, it->it_op);
169
170         if (dentry->d_name.len > EXT2_NAME_LEN)
171                 RETURN(ERR_PTR(-ENAMETOOLONG));
172
173         err = ll_lock(dir, dentry, it, &lockh);
174         if (err < 0)
175                 RETURN(ERR_PTR(err));
176         memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
177
178         request = (struct ptlrpc_request *)it->it_data;
179         if (it->it_disposition) {
180                 int mode, symlen = 0;
181                 obd_flag valid;
182
183                 offset = 1;
184                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
185                 ino = lic.lic_body->fid1.id;
186                 mode = lic.lic_body->mode;
187                 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
188                         /* For create ops, we want the lookup to be negative */
189                         if (!it->it_status)
190                                 GOTO(negative, NULL);
191                 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP)) {
192                         /* For check ops, we want the lookup to succeed */
193                         it->it_data = NULL;
194                         if (it->it_status)
195                                 GOTO(neg_req, NULL);
196                 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
197                         /* For rename, we want the lookup to succeed */
198                         if (it->it_status) {
199                                 it->it_data = NULL;
200                                 GOTO(neg_req, NULL);
201                         }
202                         it->it_data = dentry;
203                 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
204                         /* For remove ops, we want the lookup to succeed */
205                         it->it_data = NULL;
206                         if (it->it_status)
207                                 GOTO(neg_req, NULL);
208                         goto iget;
209                 } else if (it->it_op == IT_OPEN) {
210                         it->it_data = NULL;
211                         if (it->it_status && it->it_status != -EEXIST)
212                                 GOTO(neg_req, NULL);
213                 } else if (it->it_op & (IT_RENAME2|IT_LINK2)) {
214                         struct mds_body *body =
215                                 lustre_msg_buf(request->rq_repmsg, offset);
216                         it->it_data = NULL;
217                         /* For rename2, this means the lookup is negative */
218                         /* For link2 also */
219                         if (body->valid == 0)
220                                 GOTO(neg_req, NULL);
221                         goto iget;
222                 }
223
224                 /* Do a getattr now that we have the lock */
225                 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
226                 if (it->it_op == IT_READLINK) {
227                         valid |= OBD_MD_LINKNAME;
228                         symlen = lic.lic_body->size;
229                 }
230                 ptlrpc_free_req(request);
231                 request = NULL;
232                 err = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
233                                   valid, symlen, &request);
234                 if (err) {
235                         CERROR("failure %d inode %Ld\n", err, (long long)ino);
236                         GOTO(drop_req, err = -abs(err));
237                 }
238                 offset = 0;
239         } else {
240                 struct ll_inode_info *lli = ll_i2info(dir);
241                 int mode;
242
243                 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
244                 offset = 0;
245
246                 ino = ll_inode_by_name(dir, dentry, &mode);
247                 if (!ino) {
248                         CERROR("inode %*s not found by name\n",
249                                dentry->d_name.len, dentry->d_name.name);
250                         GOTO(drop_lock, err = -ENOENT);
251                 }
252
253                 err = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
254                                   OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
255                 if (err) {
256                         CERROR("failure %d inode %Ld\n", err, (long long)ino);
257                         GOTO(drop_req, err = -abs(err));
258                 }
259         }
260
261  iget:
262         lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
263         if (S_ISREG(lic.lic_body->mode) &&
264             lic.lic_body->valid & OBD_MD_FLEASIZE) {
265                 LASSERT(request->rq_repmsg->bufcount > offset);
266                 lic.lic_lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
267         } else
268                 lic.lic_lmm = NULL;
269
270         /* No rpc's happen during iget4, -ENOMEM's are possible */
271         LASSERT(ino != 0);
272         inode = iget4(dir->i_sb, ino, ll_find_inode, &lic);
273
274         if (!inode) {
275                 ptlrpc_free_req(request);
276                 ll_intent_release(dentry);
277                 RETURN(ERR_PTR(-ENOMEM));
278         }
279
280         EXIT;
281  neg_req:
282         ptlrpc_req_finished(request);
283  negative:
284         dentry->d_op = &ll_d_ops;
285         d_add(dentry, inode);
286         if (it->it_op == IT_LOOKUP)
287                 ll_intent_release(dentry);
288
289         return NULL;
290
291  drop_req:
292         ptlrpc_free_req(request);
293  drop_lock:
294 #warning FIXME: must release lock here
295         return ERR_PTR(err);
296 }
297
298 static struct inode *ll_create_node(struct inode *dir, const char *name,
299                                     int namelen, const char *tgt, int tgtlen,
300                                     int mode, __u64 extra,
301                                     struct lookup_intent *it,
302                                     struct lov_stripe_md *lsm)
303 {
304         struct inode *inode;
305         struct ptlrpc_request *request = NULL;
306         struct mds_body *body;
307         int rc;
308         time_t time = CURRENT_TIME;
309         struct ll_sb_info *sbi = ll_i2sbi(dir);
310         int gid = current->fsgid;
311         struct ll_read_inode2_cookie lic;
312         struct lov_mds_md *lmm = NULL;
313         int mds_md_size = 0;
314
315         ENTRY;
316
317         if (dir->i_mode & S_ISGID) {
318                 gid = dir->i_gid;
319                 if (S_ISDIR(mode))
320                         mode |= S_ISGID;
321         }
322
323         if (!it || !it->it_disposition) {
324                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
325                                  tgtlen, mode, current->fsuid,
326                                  gid, time, extra, lsm, &request);
327                 if (rc) {
328                         inode = ERR_PTR(rc);
329                         GOTO(out, rc);
330                 }
331                 body = lustre_msg_buf(request->rq_repmsg, 0);
332                 if (lsm != NULL) {
333                         mds_md_size = ll_mds_easize(dir->i_sb);
334                         OBD_ALLOC(lmm, mds_md_size);
335                         lov_packmd(lmm, lsm);
336                         lic.lic_lmm = lmm;
337                 } else
338                         lic.lic_lmm = NULL;
339
340         } else {
341                 invalidate_inode_pages(dir);
342                 request = it->it_data;
343                 body = lustre_msg_buf(request->rq_repmsg, 1);
344                 lic.lic_lmm = NULL;
345         }
346
347         body->valid = OBD_MD_FLNOTOBD;
348
349         body->nlink = 1;
350         body->atime = body->ctime = body->mtime = time;
351         body->uid = current->fsuid;
352         body->gid = gid;
353         body->mode = mode;
354
355         lic.lic_body = body;
356
357         inode = iget4(dir->i_sb, body->ino, ll_find_inode, &lic);
358         if (IS_ERR(inode)) {
359                 rc = PTR_ERR(inode);
360                 CERROR("new_inode -fatal: rc %d\n", rc);
361                 LBUG();
362                 GOTO(out, rc);
363         }
364
365         if (!list_empty(&inode->i_dentry)) {
366                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
367                        body->ino, atomic_read(&inode->i_count),
368                        inode->i_nlink);
369                 iput(inode);
370                 LBUG();
371                 inode = ERR_PTR(-EIO);
372                 GOTO(out, -EIO);
373         }
374
375         EXIT;
376  out:
377         if (lmm)
378                 OBD_FREE(lmm, mds_md_size);
379         ptlrpc_free_req(request);
380         return inode;
381 }
382
383 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
384                          const char *name, int len)
385 {
386         struct ptlrpc_request *request = NULL;
387         struct ll_sb_info *sbi = ll_i2sbi(dir);
388         int err;
389
390         ENTRY;
391
392         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
393                          &request);
394         ptlrpc_free_req(request);
395
396         RETURN(err);
397 }
398
399 int ll_mdc_link(struct dentry *src, struct inode *dir,
400                 const char *name, int len)
401 {
402         struct ptlrpc_request *request = NULL;
403         int err;
404         struct ll_sb_info *sbi = ll_i2sbi(dir);
405
406         ENTRY;
407
408         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name,
409                        len, &request);
410         ptlrpc_free_req(request);
411
412         RETURN(err);
413 }
414
415 int ll_mdc_rename(struct inode *src, struct inode *tgt,
416                   struct dentry *old, struct dentry *new)
417 {
418         struct ptlrpc_request *request = NULL;
419         struct ll_sb_info *sbi = ll_i2sbi(src);
420         int err;
421
422         ENTRY;
423
424         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
425                          old->d_name.name, old->d_name.len,
426                          new->d_name.name, new->d_name.len, &request);
427         ptlrpc_free_req(request);
428
429         RETURN(err);
430 }
431
432 /*
433  * By the time this is called, we already have created
434  * the directory cache entry for the new file, but it
435  * is so far negative - it has no inode.
436  *
437  * If the create succeeds, we fill in the inode information
438  * with d_instantiate().
439  */
440
441 static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
442 {
443         int err, rc = 0;
444         struct obdo oa;
445         struct inode *inode;
446         struct lov_stripe_md *lsm = NULL;
447         struct ll_inode_info *lli = NULL;
448         ENTRY;
449
450         CHECK_MOUNT_EPOCH(dir);
451
452         if (dentry->d_it->it_disposition == 0) {
453                 memset(&oa, 0, sizeof(oa));
454                 oa.o_mode = S_IFREG | 0600;
455                 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE;
456                 rc = obd_create(ll_i2obdconn(dir), &oa, &lsm);
457                 CDEBUG(D_DENTRY, "name %s mode %o o_id %lld: rc = %d\n",
458                        dentry->d_name.name, mode, (long long)oa.o_id, rc);
459                 if (rc)
460                         RETURN(rc);
461         }
462
463         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
464                                NULL, 0, mode, 0, dentry->d_it, lsm);
465
466         if (IS_ERR(inode)) {
467                 rc = PTR_ERR(inode);
468                 CERROR("error creating MDS object for id %Ld: rc = %d\n",
469                        (unsigned long long)oa.o_id, rc);
470                 GOTO(out_destroy, rc);
471         }
472
473         if (dentry->d_it->it_disposition) {
474                 lli = ll_i2info(inode);
475                 memcpy(&lli->lli_intent_lock_handle,
476                        dentry->d_it->it_lock_handle,
477                        sizeof(struct lustre_handle));
478                 d_instantiate(dentry, inode);
479         } else {
480                 /* no directory data updates when intents rule */
481                 rc = ext2_add_nondir(dentry, inode);
482         }
483
484         RETURN(rc);
485
486 out_destroy:
487         if (lsm) {
488                 oa.o_easize = ll_mds_easize(inode->i_sb);
489                 oa.o_valid |= OBD_MD_FLEASIZE;
490                 err = obd_destroy(ll_i2obdconn(dir), &oa, lsm);
491                 if (err)
492                         CERROR("error destroying objid %Ld on error: err %d\n",
493                        (unsigned long long)oa.o_id, err);
494         }
495
496         return rc;
497 }
498
499 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
500                     int rdev)
501 {
502         struct inode *inode;
503         int err = 0;
504
505         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
506                                NULL, 0, mode, rdev, dentry->d_it, NULL);
507
508         if (IS_ERR(inode))
509                 RETURN(PTR_ERR(inode));
510
511         /* no directory data updates when intents rule */
512         if (dentry->d_it && dentry->d_it->it_disposition)
513                 d_instantiate(dentry, inode);
514         else
515                 err = ext2_add_nondir(dentry, inode);
516
517         return err;
518 }
519
520 static int ll_symlink(struct inode *dir, struct dentry *dentry,
521                       const char *symname)
522 {
523         unsigned l = strlen(symname);
524         struct inode *inode;
525         struct ll_inode_info *lli;
526         int err = 0;
527         ENTRY;
528
529         CHECK_MOUNT_EPOCH(dir);
530
531         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
532                                symname, l, S_IFLNK | S_IRWXUGO, 0,
533                                dentry->d_it, NULL);
534         if (IS_ERR(inode))
535                 RETURN(PTR_ERR(inode));
536
537         lli = ll_i2info(inode);
538
539         OBD_ALLOC(lli->lli_symlink_name, l + 1);
540         /* this _could_ be a non-fatal error, since the symlink is already
541          * stored on the MDS by this point, and we can re-get it in readlink.
542          */
543         if (!lli->lli_symlink_name)
544                 RETURN(-ENOMEM);
545
546         memcpy(lli->lli_symlink_name, symname, l + 1);
547         inode->i_size = l;
548
549         /* no directory data updates when intents rule */
550         if (dentry->d_it && dentry->d_it->it_disposition)
551                 d_instantiate(dentry, inode);
552         else
553                 err = ext2_add_nondir(dentry, inode);
554
555         RETURN(err);
556 }
557
558 static int ll_link(struct dentry *old_dentry, struct inode * dir,
559                    struct dentry *dentry)
560 {
561         int err;
562         struct inode *inode = old_dentry->d_inode;
563
564         if (dentry->d_it && dentry->d_it->it_disposition) { 
565                 int err = dentry->d_it->it_status;
566                 if (err) 
567                         RETURN(err);
568                 inode->i_ctime = CURRENT_TIME;
569                 ext2_inc_count(inode);
570                 atomic_inc(&inode->i_count);
571                 d_instantiate(dentry, inode);
572                 invalidate_inode_pages(dir);
573                 RETURN(err);
574         }
575
576         if (S_ISDIR(inode->i_mode))
577                 return -EPERM;
578
579         if (inode->i_nlink >= EXT2_LINK_MAX)
580                 return -EMLINK;
581
582         err = ll_mdc_link(old_dentry, dir,
583                           dentry->d_name.name, dentry->d_name.len);
584         if (err)
585                 RETURN(err);
586
587         inode->i_ctime = CURRENT_TIME;
588         ext2_inc_count(inode);
589         atomic_inc(&inode->i_count);
590
591         return ext2_add_nondir(dentry, inode);
592 }
593
594 static int ll_mkdir(struct inode * dir, struct dentry * dentry, int mode)
595 {
596         struct inode * inode;
597         int err = -EMLINK;
598         ENTRY;
599
600         if (dir->i_nlink >= EXT2_LINK_MAX)
601                 goto out;
602
603         ext2_inc_count(dir);
604
605         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
606                                NULL, 0, S_IFDIR | mode, 0, dentry->d_it, NULL);
607         err = PTR_ERR(inode);
608         if (IS_ERR(inode))
609                 goto out_dir;
610
611         ext2_inc_count(inode);
612
613         err = ext2_make_empty(inode, dir);
614         if (err)
615                 goto out_fail;
616
617         /* no directory data updates when intents rule */
618         if (dentry->d_it->it_disposition == 0) {
619                 err = ll_add_link(dentry, inode);
620                 if (err)
621                         goto out_fail;
622         }
623
624         d_instantiate(dentry, inode);
625 out:
626         EXIT;
627         return err;
628
629 out_fail:
630         ext2_dec_count(inode);
631         ext2_dec_count(inode);
632         iput(inode);
633         EXIT;
634 out_dir:
635         ext2_dec_count(dir);
636         EXIT;
637         goto out;
638 }
639
640 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
641                             __u32 mode)
642 {
643         struct inode * inode = dentry->d_inode;
644 #if 0
645         struct ext2_dir_entry_2 * de;
646         struct page * page;
647 #endif
648         int err = -ENOENT;
649
650         if (dentry->d_it && dentry->d_it->it_disposition) {
651                 err = dentry->d_it->it_status;
652                 invalidate_inode_pages(dir);
653                 GOTO(out, err);
654         }
655
656 #if 0
657         de = ext2_find_entry(dir, dentry, &page);
658         if (!de)
659                 goto out;
660 #endif
661         err = ll_mdc_unlink(dir, dentry->d_inode, mode,
662                             dentry->d_name.name, dentry->d_name.len);
663         if (err)
664                 goto out;
665
666 #if 0
667         err = ext2_delete_entry(de, page);
668         if (err)
669                 goto out;
670 #endif
671         invalidate_inode_pages(dir);
672
673         inode->i_ctime = dir->i_ctime;
674 out:
675         ext2_dec_count(inode);
676         return err;
677 }
678
679 static int ll_unlink(struct inode *dir, struct dentry *dentry)
680 {
681         return ll_common_unlink(dir, dentry, S_IFREG);
682 }
683
684 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
685 {
686         struct inode * inode = dentry->d_inode;
687         int err = 0;
688         ENTRY;
689
690         if (!dentry->d_it || dentry->d_it->it_disposition == 0) {
691                 if (!ext2_empty_dir(inode))
692                         LBUG();
693                 err = ll_common_unlink(dir, dentry, S_IFDIR);
694         } else
695                 err = dentry->d_it->it_status;
696         if (err)
697                 RETURN(err);
698         inode->i_size = 0;
699         ext2_dec_count(inode);
700         ext2_dec_count(dir);
701         RETURN(err);
702 }
703
704 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
705                      struct inode * new_dir, struct dentry * new_dentry)
706 {
707         struct inode * old_inode = old_dentry->d_inode;
708         struct inode * tgt_inode = new_dentry->d_inode;
709         struct page * dir_page = NULL;
710         struct ext2_dir_entry_2 * dir_de = NULL;
711         struct ext2_dir_entry_2 * old_de;
712         struct page * old_page;
713         int err = -ENOENT;
714
715         if (new_dentry->d_it && new_dentry->d_it->it_disposition) { 
716                 if (tgt_inode) {
717                         tgt_inode->i_ctime = CURRENT_TIME;
718                         tgt_inode->i_nlink--;
719                 }
720                 invalidate_inode_pages(old_dir);
721                 invalidate_inode_pages(new_dir);
722                 GOTO(out, err = new_dentry->d_it->it_status);
723         }
724
725         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
726         if (err)
727                 goto out;
728
729         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
730         if (!old_de)
731                 goto out;
732
733         if (S_ISDIR(old_inode->i_mode)) {
734                 err = -EIO;
735                 dir_de = ext2_dotdot(old_inode, &dir_page);
736                 if (!dir_de)
737                         goto out_old;
738         }
739
740         if (tgt_inode) {
741                 struct page *new_page;
742                 struct ext2_dir_entry_2 *new_de;
743
744                 err = -ENOTEMPTY;
745                 if (dir_de && !ext2_empty_dir (tgt_inode))
746                         goto out_dir;
747
748                 err = -ENOENT;
749                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
750                 if (!new_de)
751                         goto out_dir;
752                 ext2_inc_count(old_inode);
753                 ext2_set_link(new_dir, new_de, new_page, old_inode);
754                 tgt_inode->i_ctime = CURRENT_TIME;
755                 if (dir_de)
756                         tgt_inode->i_nlink--;
757                 ext2_dec_count(tgt_inode);
758         } else {
759                 if (dir_de) {
760                         err = -EMLINK;
761                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
762                                 goto out_dir;
763                 }
764                 ext2_inc_count(old_inode);
765                 err = ll_add_link(new_dentry, old_inode);
766                 if (err) {
767                         ext2_dec_count(old_inode);
768                         goto out_dir;
769                 }
770                 if (dir_de)
771                         ext2_inc_count(new_dir);
772         }
773
774         ext2_delete_entry (old_de, old_page);
775         ext2_dec_count(old_inode);
776
777         if (dir_de) {
778                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
779                 ext2_dec_count(old_dir);
780         }
781         return 0;
782
783 out_dir:
784         if (dir_de) {
785                 kunmap(dir_page);
786                 page_cache_release(dir_page);
787         }
788 out_old:
789         kunmap(old_page);
790         page_cache_release(old_page);
791 out:
792         return err;
793 }
794
795 struct inode_operations ll_dir_inode_operations = {
796         create:         ll_create,
797         lookup2:        ll_lookup2,
798         link:           ll_link,
799         unlink:         ll_unlink,
800         symlink:        ll_symlink,
801         mkdir:          ll_mkdir,
802         rmdir:          ll_rmdir,
803         mknod:          ll_mknod,
804         rename:         ll_rename,
805         setattr:        ll_setattr
806 };