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