Whamcloud - gitweb
- osc_create was setting lmd_stripe_count to 1 instead of 0
[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, 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;
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, easize = 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                 md.body = lustre_msg_buf(request->rq_repmsg, offset);
213                 ino = md.body->fid1.id;
214                 mode = md.body->mode;
215                 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
216                 if (it->it_op == IT_READLINK) {
217                         valid |= OBD_MD_LINKNAME;
218                         easize = md.body->size;
219                 }
220                 ptlrpc_free_req(request);
221                 request = NULL;
222                 err = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
223                                   valid, easize, &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         md.body = lustre_msg_buf(request->rq_repmsg, offset);
253         if (S_ISREG(md.body->mode)) {
254                 if (request->rq_repmsg->bufcount < offset + 1)
255                         LBUG();
256                 md.md = lustre_msg_buf(request->rq_repmsg, offset + 1);
257                 if (md.md->lmd_magic != LOV_MAGIC)
258                         md.md = NULL;
259         } else
260                 md.md = NULL;
261
262         /* No rpc's happen during iget4, -ENOMEM's are possible */
263         inode = iget4(dir->i_sb, ino, ll_find_inode, &md);
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 *smd)
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_inode_md md;
298         struct lov_mds_md *mds_md = 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, smd, &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 (smd != NULL) {
319                         mds_md_size = sizeof (struct lov_mds_md) + 
320                                 smd->lmd_stripe_count * sizeof(struct lov_object_id);
321                         OBD_ALLOC(mds_md, mds_md_size);
322                         lov_packmd(mds_md, smd);
323                         md.md = mds_md;
324                 } else
325                         md.md = NULL;
326
327         } else {
328                 request = it->it_data;
329                 body = lustre_msg_buf(request->rq_repmsg, 1);
330                 md.md = 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         md.body = body;
342
343         inode = iget4(dir->i_sb, body->ino, ll_find_inode, &md);
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 (mds_md != NULL) 
364                 OBD_FREE(mds_md, 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 *smd = 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, &smd);
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, smd);
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 (smd) {
472                 oa.o_easize = smd->lmd_mds_easize;
473                 oa.o_valid |= OBD_MD_FLEASIZE;
474                 err = obd_destroy(ll_i2obdconn(dir), &oa, smd);
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 #warning FIXME: still needs intent support
547         if (S_ISDIR(inode->i_mode))
548                 return -EPERM;
549
550         if (inode->i_nlink >= EXT2_LINK_MAX)
551                 return -EMLINK;
552
553         err = ll_mdc_link(old_dentry, dir,
554                           dentry->d_name.name, dentry->d_name.len);
555         if (err)
556                 RETURN(err);
557
558         inode->i_ctime = CURRENT_TIME;
559         ext2_inc_count(inode);
560         atomic_inc(&inode->i_count);
561
562         return ext2_add_nondir(dentry, inode);
563 }
564
565 static int ll_mkdir(struct inode * dir, struct dentry * dentry, int mode)
566 {
567         struct inode * inode;
568         int err = -EMLINK;
569         ENTRY;
570
571         if (dir->i_nlink >= EXT2_LINK_MAX)
572                 goto out;
573
574         ext2_inc_count(dir);
575
576         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
577                                NULL, 0, S_IFDIR | mode, 0, dentry->d_it, NULL);
578         err = PTR_ERR(inode);
579         if (IS_ERR(inode))
580                 goto out_dir;
581
582         ext2_inc_count(inode);
583
584         err = ext2_make_empty(inode, dir);
585         if (err)
586                 goto out_fail;
587
588         /* no directory data updates when intents rule */
589         if (dentry->d_it->it_disposition == 0) {
590                 err = ll_add_link(dentry, inode);
591                 if (err)
592                         goto out_fail;
593         }
594
595         d_instantiate(dentry, inode);
596 out:
597         EXIT;
598         return err;
599
600 out_fail:
601         ext2_dec_count(inode);
602         ext2_dec_count(inode);
603         iput(inode);
604         EXIT;
605 out_dir:
606         ext2_dec_count(dir);
607         EXIT;
608         goto out;
609 }
610
611 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
612                             __u32 mode)
613 {
614         struct inode * inode = dentry->d_inode;
615         struct ext2_dir_entry_2 * de;
616         struct page * page;
617         int err = -ENOENT;
618
619         if (dentry->d_it && dentry->d_it->it_disposition) {
620                 err = dentry->d_it->it_status;
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
658                 err = ll_common_unlink(dir, dentry, S_IFDIR);
659         } else
660                 err = dentry->d_it->it_status;
661         if (err)
662                 RETURN(err);
663         inode->i_size = 0;
664         ext2_dec_count(inode);
665         ext2_dec_count(dir);
666         RETURN(err);
667 }
668
669 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
670                      struct inode * new_dir, struct dentry * new_dentry)
671 {
672         struct inode * old_inode = old_dentry->d_inode;
673         struct inode * new_inode = new_dentry->d_inode;
674         struct page * dir_page = NULL;
675         struct ext2_dir_entry_2 * dir_de = NULL;
676         struct ext2_dir_entry_2 * old_de;
677         struct page * old_page;
678         int err = -ENOENT;
679
680         if (new_dentry->d_it && new_dentry->d_it->it_disposition) { 
681                 if (new_inode) {
682                         new_inode->i_ctime = CURRENT_TIME;
683                         new_inode->i_nlink--;
684                 }
685                 GOTO(out, err = new_dentry->d_it->it_status);
686         }
687
688         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
689         if (err)
690                 goto out;
691
692         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
693         if (!old_de)
694                 goto out;
695
696         if (S_ISDIR(old_inode->i_mode)) {
697                 err = -EIO;
698                 dir_de = ext2_dotdot(old_inode, &dir_page);
699                 if (!dir_de)
700                         goto out_old;
701         }
702
703         if (new_inode) {
704                 struct page *new_page;
705                 struct ext2_dir_entry_2 *new_de;
706
707                 err = -ENOTEMPTY;
708                 if (dir_de && !ext2_empty_dir (new_inode))
709                         goto out_dir;
710
711                 err = -ENOENT;
712                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
713                 if (!new_de)
714                         goto out_dir;
715                 ext2_inc_count(old_inode);
716                 ext2_set_link(new_dir, new_de, new_page, old_inode);
717                 new_inode->i_ctime = CURRENT_TIME;
718                 if (dir_de)
719                         new_inode->i_nlink--;
720                 ext2_dec_count(new_inode);
721         } else {
722                 if (dir_de) {
723                         err = -EMLINK;
724                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
725                                 goto out_dir;
726                 }
727                 ext2_inc_count(old_inode);
728                 err = ll_add_link(new_dentry, old_inode);
729                 if (err) {
730                         ext2_dec_count(old_inode);
731                         goto out_dir;
732                 }
733                 if (dir_de)
734                         ext2_inc_count(new_dir);
735         }
736
737         ext2_delete_entry (old_de, old_page);
738         ext2_dec_count(old_inode);
739
740         if (dir_de) {
741                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
742                 ext2_dec_count(old_dir);
743         }
744         return 0;
745
746 out_dir:
747         if (dir_de) {
748                 kunmap(dir_page);
749                 page_cache_release(dir_page);
750         }
751 out_old:
752         kunmap(old_page);
753         page_cache_release(old_page);
754 out:
755         return err;
756 }
757
758 struct inode_operations ll_dir_inode_operations = {
759         create:         ll_create,
760         lookup2:        ll_lookup2,
761         link:           ll_link,
762         unlink:         ll_unlink,
763         symlink:        ll_symlink,
764         mkdir:          ll_mkdir,
765         rmdir:          ll_rmdir,
766         mknod:          ll_mknod,
767         rename:         ll_rename,
768         setattr:        ll_setattr
769 };