Whamcloud - gitweb
- buffer checks are different for non-intent
[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                 ll_set_dd(dentry);
294         } else
295                 CERROR("NOT allocating fsdata - already set\n");
296
297         d_add(dentry, inode);
298
299         if (it->it_status == 0) {
300                 LL_SAVE_INTENT(dentry, it);
301         }
302         else {
303                 dentry->d_it = NULL;
304                 CDEBUG(D_DENTRY,
305                        "D_IT dentry %p fsdata %p intent: %s status %d\n",
306                        dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
307                        it->it_status);
308         }
309
310         if (it->it_op == IT_LOOKUP)
311                 ll_intent_release(dentry, it);
312
313         return NULL;
314
315  drop_req:
316         ptlrpc_free_req(request);
317  drop_lock:
318 #warning FIXME: must release lock here
319         return ERR_PTR(rc);
320 }
321
322 static struct inode *ll_create_node(struct inode *dir, const char *name,
323                                     int namelen, const char *tgt, int tgtlen,
324                                     int mode, __u64 extra,
325                                     struct lookup_intent *it,
326                                     struct lov_stripe_md *lsm)
327 {
328         struct inode *inode;
329         struct ptlrpc_request *request = NULL;
330         struct mds_body *body;
331         time_t time = CURRENT_TIME;
332         struct ll_sb_info *sbi = ll_i2sbi(dir);
333         struct ll_read_inode2_cookie lic;
334         struct lov_mds_md *lmm = NULL;
335         ENTRY;
336
337         if (it && it->it_disposition) {
338                 int rc = it->it_status;
339                 if (rc) {
340                         CERROR("error creating MDS inode for %*s: rc = %d\n",
341                                namelen, name, rc);
342                         RETURN(ERR_PTR(rc));
343                 }
344                 invalidate_inode_pages(dir);
345                 request = it->it_data;
346                 body = lustre_msg_buf(request->rq_repmsg, 1);
347                 lic.lic_lmm = NULL;
348         } else {
349                 int gid = current->fsgid;
350                 int rc;
351
352                 if (lsm) {
353                         OBD_ALLOC(lmm, lsm->lsm_mds_easize);
354                         if (!lmm)
355                                 RETURN(ERR_PTR(-ENOMEM));
356                         lov_packmd(lmm, lsm);
357                         lic.lic_lmm = lmm;
358                 } else
359                         lic.lic_lmm = NULL;
360
361                 if (dir->i_mode & S_ISGID) {
362                         gid = dir->i_gid;
363                         if (S_ISDIR(mode))
364                                 mode |= S_ISGID;
365                 }
366
367                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
368                                 tgtlen, mode, current->fsuid, gid,
369                                 time, extra, lsm, &request);
370                 if (rc) {
371                         inode = ERR_PTR(rc);
372                         GOTO(out, rc);
373                 }
374                 body = lustre_msg_buf(request->rq_repmsg, 0);
375         }
376
377         lic.lic_body = body;
378
379         LASSERT(body->ino != 0);
380         inode = iget4(dir->i_sb, body->ino, ll_find_inode, &lic);
381         if (IS_ERR(inode)) {
382                 int rc = PTR_ERR(inode);
383                 CERROR("new_inode -fatal: rc %d\n", rc);
384                 LBUG();
385                 GOTO(out, rc);
386         }
387
388         if (!list_empty(&inode->i_dentry)) {
389                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
390                        body->ino, atomic_read(&inode->i_count),
391                        inode->i_nlink);
392                 iput(inode);
393                 LBUG();
394                 inode = ERR_PTR(-EIO);
395                 GOTO(out, -EIO);
396         }
397
398         EXIT;
399  out:
400         if (lsm && lmm)
401                 OBD_FREE(lmm, lsm->lsm_mds_easize);
402         ptlrpc_req_finished(request);
403         return inode;
404 }
405
406 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
407                          const char *name, int len)
408 {
409         struct ptlrpc_request *request = NULL;
410         struct ll_sb_info *sbi = ll_i2sbi(dir);
411         int err;
412
413         ENTRY;
414
415         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
416                          &request);
417         ptlrpc_req_finished(request);
418
419         RETURN(err);
420 }
421
422 int ll_mdc_link(struct dentry *src, struct inode *dir,
423                 const char *name, int len)
424 {
425         struct ptlrpc_request *request = NULL;
426         int err;
427         struct ll_sb_info *sbi = ll_i2sbi(dir);
428
429         ENTRY;
430
431         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
432         ptlrpc_req_finished(request);
433
434         RETURN(err);
435 }
436
437 int ll_mdc_rename(struct inode *src, struct inode *tgt,
438                   struct dentry *old, struct dentry *new)
439 {
440         struct ptlrpc_request *request = NULL;
441         struct ll_sb_info *sbi = ll_i2sbi(src);
442         int err;
443
444         ENTRY;
445
446         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
447                          old->d_name.name, old->d_name.len,
448                          new->d_name.name, new->d_name.len, &request);
449         ptlrpc_req_finished(request);
450
451         RETURN(err);
452 }
453
454 /*
455  * By the time this is called, we already have created the directory cache
456  * entry for the new file, but it is so far negative - it has no inode.
457  * We defer creating the OBD object(s) until open, to keep the intent and
458  * non-intent code paths similar, and also because we do not have the MDS
459  * inode number before calling ll_create_node() (which is needed for LOV),
460  * so we would need to do yet another RPC to the MDS to store the LOV EA
461  * data on the MDS.
462  *
463  * If the create succeeds, we fill in the inode information
464  * with d_instantiate().
465  */
466 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
467 {
468         struct lookup_intent *it;
469         struct inode *inode;
470         int rc = 0;
471         ENTRY;
472
473         CHECK_MOUNT_EPOCH(dir);
474
475         LL_GET_INTENT(dentry, it);
476
477         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
478                                NULL, 0, mode, 0, it, NULL);
479
480         if (IS_ERR(inode))
481                 RETURN(PTR_ERR(inode));
482
483         if (it->it_disposition) {
484                 struct ll_inode_info *lli = ll_i2info(inode);
485                 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
486                        sizeof(lli->lli_intent_lock_handle));
487                 d_instantiate(dentry, inode);
488         } else {
489                 /* no directory data updates when intents rule */
490                 rc = ext2_add_nondir(dentry, inode);
491         }
492
493         RETURN(rc);
494 }
495
496 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
497                     int rdev)
498 {
499         struct lookup_intent *it;
500         struct inode *inode;
501         int rc = 0;
502         
503         LL_GET_INTENT(dentry, it);
504
505         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
506                                NULL, 0, mode, rdev, it, NULL);
507
508         if (IS_ERR(inode))
509                 RETURN(PTR_ERR(inode));
510
511         /* no directory data updates when intents rule */
512         if (it && it->it_disposition)
513                 d_instantiate(dentry, inode);
514         else
515                 rc = ext2_add_nondir(dentry, inode);
516
517         return rc;
518 }
519
520 static int ll_symlink(struct inode *dir, struct dentry *dentry,
521                       const char *symname)
522 {
523         struct lookup_intent *it;
524         unsigned l = strlen(symname);
525         struct inode *inode;
526         struct ll_inode_info *lli;
527         int err = 0;
528         ENTRY;
529
530         CHECK_MOUNT_EPOCH(dir);
531
532         LL_GET_INTENT(dentry, it);
533
534         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
535                                symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
536         if (IS_ERR(inode))
537                 RETURN(PTR_ERR(inode));
538
539         lli = ll_i2info(inode);
540
541         OBD_ALLOC(lli->lli_symlink_name, l + 1);
542         /* this _could_ be a non-fatal error, since the symlink is already
543          * stored on the MDS by this point, and we can re-get it in readlink.
544          */
545         if (!lli->lli_symlink_name)
546                 RETURN(-ENOMEM);
547
548         memcpy(lli->lli_symlink_name, symname, l + 1);
549         inode->i_size = l;
550
551         /* no directory data updates when intents rule */
552         if (it && it->it_disposition)
553                 d_instantiate(dentry, inode);
554         else
555                 err = ext2_add_nondir(dentry, inode);
556
557         RETURN(err);
558 }
559
560 static int ll_link(struct dentry *old_dentry, struct inode * dir,
561                    struct dentry *dentry)
562 {
563         struct lookup_intent *it;
564         struct inode *inode = old_dentry->d_inode;
565         int rc;
566         
567         LL_GET_INTENT(dentry, it);
568
569         if (it && it->it_disposition) {
570                 if (it->it_status)
571                         RETURN(it->it_status);
572                 inode->i_ctime = CURRENT_TIME;
573                 ext2_inc_count(inode);
574                 atomic_inc(&inode->i_count);
575                 d_instantiate(dentry, inode);
576                 invalidate_inode_pages(dir);
577                 RETURN(0);
578         }
579
580         if (S_ISDIR(inode->i_mode))
581                 return -EPERM;
582
583         if (inode->i_nlink >= EXT2_LINK_MAX)
584                 return -EMLINK;
585
586         rc = ll_mdc_link(old_dentry, dir,
587                           dentry->d_name.name, dentry->d_name.len);
588         if (rc)
589                 RETURN(rc);
590
591         inode->i_ctime = CURRENT_TIME;
592         ext2_inc_count(inode);
593         atomic_inc(&inode->i_count);
594
595         return ext2_add_nondir(dentry, inode);
596 }
597
598 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
599 {
600         struct lookup_intent *it;
601         struct inode * inode;
602         int err = -EMLINK;
603         ENTRY;
604
605         LL_GET_INTENT(dentry, it);
606
607         if (dir->i_nlink >= EXT2_LINK_MAX)
608                 goto out;
609
610         ext2_inc_count(dir);
611         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
612                                NULL, 0, S_IFDIR | mode, 0, it, NULL);
613         err = PTR_ERR(inode);
614         if (IS_ERR(inode))
615                 goto out_dir;
616
617         ext2_inc_count(inode);
618
619         err = ext2_make_empty(inode, dir);
620         if (err)
621                 goto out_fail;
622
623         /* no directory data updates when intents rule */
624         if (!it || !it->it_disposition) {
625                 err = ll_add_link(dentry, inode);
626                 if (err)
627                         goto out_fail;
628         }
629
630         d_instantiate(dentry, inode);
631 out:
632         EXIT;
633         return err;
634
635 out_fail:
636         ext2_dec_count(inode);
637         ext2_dec_count(inode);
638         iput(inode);
639         EXIT;
640 out_dir:
641         ext2_dec_count(dir);
642         EXIT;
643         goto out;
644 }
645
646 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
647                             struct lookup_intent *it, __u32 mode)
648 {
649         struct inode *inode = dentry->d_inode;
650         struct ext2_dir_entry_2 * de;
651         struct page * page;
652         int rc = 0;
653
654         if (it && it->it_disposition) {
655                 rc = it->it_status;
656                 invalidate_inode_pages(dir);
657                 if (rc)
658                         GOTO(out, rc);
659                 GOTO(out_dec, 0);
660         }
661
662         de = ext2_find_entry(dir, dentry, &page);
663         if (!de)
664                 GOTO(out, rc = -ENOENT);
665         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
666                            dentry->d_name.name, dentry->d_name.len);
667         if (rc)
668                 GOTO(out, rc);
669
670         rc = ext2_delete_entry(de, page);
671         if (rc)
672                 GOTO(out, rc);
673
674         /* AED: not sure if needed - directory lock revocation should do it
675          * in the case where the client has cached it for non-intent ops.
676          */
677         invalidate_inode_pages(dir);
678
679         inode->i_ctime = dir->i_ctime;
680 out_dec:
681         ext2_dec_count(inode);
682 out:
683         return rc;
684 }
685
686 static int ll_unlink(struct inode *dir, struct dentry *dentry)
687 {
688         struct lookup_intent * it;
689
690         LL_GET_INTENT(dentry, it);
691
692         return ll_common_unlink(dir, dentry, it, S_IFREG);
693 }
694
695 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
696 {
697         struct inode * inode = dentry->d_inode;
698         struct lookup_intent *it;
699         int rc;
700         ENTRY;
701
702         LL_GET_INTENT(dentry, it);
703
704
705         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
706                 RETURN(-ENOTEMPTY);
707
708         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
709         if (!rc) {
710                 inode->i_size = 0;
711                 ext2_dec_count(inode);
712                 ext2_dec_count(dir);
713         }
714
715         RETURN(rc);
716 }
717
718 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
719                      struct inode * new_dir, struct dentry * new_dentry)
720 {
721         struct lookup_intent *it;
722         struct inode * old_inode = old_dentry->d_inode;
723         struct inode * tgt_inode = new_dentry->d_inode;
724         struct page * dir_page = NULL;
725         struct ext2_dir_entry_2 * dir_de = NULL;
726         struct ext2_dir_entry_2 * old_de;
727         struct page * old_page;
728         int err;
729
730         LL_GET_INTENT(new_dentry, it);
731
732         if (it && it->it_disposition) { 
733                 if (tgt_inode) {
734                         tgt_inode->i_ctime = CURRENT_TIME;
735                         tgt_inode->i_nlink--;
736                 }
737                 invalidate_inode_pages(old_dir);
738                 invalidate_inode_pages(new_dir);
739                 GOTO(out, err = it->it_status);
740         }
741
742         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
743         if (err)
744                 goto out;
745
746         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
747         if (!old_de)
748                 goto out;
749
750         if (S_ISDIR(old_inode->i_mode)) {
751                 err = -EIO;
752                 dir_de = ext2_dotdot(old_inode, &dir_page);
753                 if (!dir_de)
754                         goto out_old;
755         }
756
757         if (tgt_inode) {
758                 struct page *new_page;
759                 struct ext2_dir_entry_2 *new_de;
760
761                 err = -ENOTEMPTY;
762                 if (dir_de && !ext2_empty_dir (tgt_inode))
763                         goto out_dir;
764
765                 err = -ENOENT;
766                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
767                 if (!new_de)
768                         goto out_dir;
769                 ext2_inc_count(old_inode);
770                 ext2_set_link(new_dir, new_de, new_page, old_inode);
771                 tgt_inode->i_ctime = CURRENT_TIME;
772                 if (dir_de)
773                         tgt_inode->i_nlink--;
774                 ext2_dec_count(tgt_inode);
775         } else {
776                 if (dir_de) {
777                         err = -EMLINK;
778                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
779                                 goto out_dir;
780                 }
781                 ext2_inc_count(old_inode);
782                 err = ll_add_link(new_dentry, old_inode);
783                 if (err) {
784                         ext2_dec_count(old_inode);
785                         goto out_dir;
786                 }
787                 if (dir_de)
788                         ext2_inc_count(new_dir);
789         }
790
791         ext2_delete_entry (old_de, old_page);
792         ext2_dec_count(old_inode);
793
794         if (dir_de) {
795                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
796                 ext2_dec_count(old_dir);
797         }
798         return 0;
799
800 out_dir:
801         if (dir_de) {
802                 kunmap(dir_page);
803                 page_cache_release(dir_page);
804         }
805 out_old:
806         kunmap(old_page);
807         page_cache_release(old_page);
808 out:
809         return err;
810 }
811
812 struct inode_operations ll_dir_inode_operations = {
813         create:         ll_create,
814         lookup2:        ll_lookup2,
815         link:           ll_link,
816         unlink:         ll_unlink,
817         symlink:        ll_symlink,
818         mkdir:          ll_mkdir,
819         rmdir:          ll_rmdir,
820         mknod:          ll_mknod,
821         rename:         ll_rename,
822         setattr:        ll_setattr
823 };