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