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