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