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