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