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