Whamcloud - gitweb
Lproc-snmp code drop
[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
92 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
93 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
94 #else
95 static int ll_test_inode(struct inode *inode, void *opaque)
96 #endif
97 {
98         struct ll_read_inode2_cookie *lic = opaque;
99         struct mds_body *body = lic->lic_body;
100
101         if (inode->i_generation != lic->lic_body->generation)
102                 return 0;
103
104         /* Apply the attributes in 'opaque' to this inode */
105         ll_update_inode(inode, body);
106
107         return 1;
108 }
109
110 extern struct dentry_operations ll_d_ops;
111
112 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
113 {
114         ENTRY;
115
116         ldlm_lock_decref(lockh, mode);
117
118         RETURN(0);
119 }
120
121 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
122 extern int ll_read_inode2(struct inode *inode, void *opaque);
123 struct inode *ll_iget(struct super_block *sb, ino_t hash,
124                       struct ll_read_inode2_cookie *lic)
125 {
126         struct inode *inode;
127
128         inode = iget5_locked(sb, hash, ll_test_inode, ll_read_inode2, lic);
129
130         if (!inode)
131                 return ERR_PTR(-ENOMEM);
132
133         if (inode->i_state & I_NEW) {
134
135                 unlock_new_inode(inode);
136         }
137
138         // XXX Coda always fills inodes, should Lustre?
139         return inode;
140 }
141 #else
142 struct inode *ll_iget(struct super_block *sb, ino_t hash,
143                       struct ll_read_inode2_cookie *lic)
144 {
145         struct inode *inode;
146         inode = iget4(sb, hash, ll_find_inode, lic);
147         return inode;
148 }
149 #endif
150
151 static int ll_intent_to_lock_mode(struct lookup_intent *it)
152 {
153         /* CREAT needs to be tested before open (both could be set) */
154         if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SETATTR | IT_MKNOD))) {
155                 return LCK_PW;
156         } else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
157                                 IT_RMDIR | IT_RENAME | IT_RENAME2 | IT_READLINK|
158                                 IT_LINK | IT_LINK2 | IT_LOOKUP | IT_SYMLINK)) {
159                 return LCK_PW;
160         }
161
162         LBUG();
163         RETURN(-EINVAL);
164 }
165
166 #define LL_LOOKUP_POSITIVE 1
167 #define LL_LOOKUP_NEGATIVE 2
168
169 int ll_intent_lock(struct inode *parent, struct dentry **de,
170                    struct lookup_intent *it,
171                    intent_finish_cb intent_finish)
172 {
173         struct dentry *dentry = *de;
174         struct ll_sb_info *sbi = ll_i2sbi(parent);
175         struct lustre_handle lockh;
176         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
177         struct ptlrpc_request *request = NULL;
178         char *tgt = NULL;
179         int rc, lock_mode, tgtlen = 0, offset, flag = LL_LOOKUP_POSITIVE;
180         obd_id ino = 0;
181
182         ENTRY;
183
184         if (it == NULL)
185                 it = &lookup_it;
186
187         CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
188                dentry->d_name.name, ldlm_it2str(it->it_op));
189
190         if (dentry->d_name.len > EXT2_NAME_LEN)
191                 RETURN(-ENAMETOOLONG);
192
193         lock_mode = ll_intent_to_lock_mode(it);
194         if (it->it_op & IT_SYMLINK) {
195                 tgt = it->it_data;
196                 tgtlen = strlen(tgt);
197                 it->it_data = NULL;
198         }
199
200         rc = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_PLAIN, it, lock_mode, parent,
201                          dentry, &lockh, tgt, tgtlen, parent, sizeof(*parent));
202         if (rc < 0)
203                 RETURN(rc);
204         memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
205
206         request = (struct ptlrpc_request *)it->it_data;
207         if (it->it_disposition) {
208                 struct mds_body *mds_body;
209                 int mode, symlen = 0;
210                 obd_flag valid;
211
212                 /* it_disposition == 1 indicates that the server performed the
213                  * intent on our behalf.  This long block is all about fixing up
214                  * the local state so that it is correct as of the moment
215                  * _before_ the operation was applied; that way, the VFS will
216                  * think that everything is normal and call Lustre's regular
217                  * FS function.
218                  *
219                  * If we're performing a creation, that means that unless the
220                  * creation failed with EEXIST, we should fake up a negative
221                  * dentry.  Likewise for the target of a hard link.
222                  *
223                  * For everything else, we want to lookup to succeed. */
224
225                 /* One additional note: we add an extra reference to the request
226                  * because we need to keep it around until ll_create gets
227                  * called.  For anything else which results in
228                  * LL_LOOKUP_POSITIVE, we can do the iget() immediately with the
229                  * contents of the reply (in the intent_finish callback).  In
230                  * the create case, however, we need to wait until
231                  * ll_create_node to do the iget() or the VFS will abort with
232                  * -EEXISTS. */
233
234                 offset = 1;
235                 mds_body = lustre_msg_buf(request->rq_repmsg, offset);
236                 ino = mds_body->fid1.id;
237                 mode = mds_body->mode;
238                 if (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) {
239                         mdc_store_create_replay_data(request, parent->i_sb);
240                         /* For create ops, we want the lookup to be negative,
241                          * unless the create failed in a way that indicates
242                          * that the file is already there */
243                         if (it->it_status != -EEXIST) {
244                                 atomic_inc(&request->rq_refcount);
245                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
246                         }
247                         /* Fall through to update attibutes. */
248                 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
249                                         IT_READLINK)) {
250                         /* For check ops, we want the lookup to succeed */
251                         it->it_data = NULL;
252                         if (it->it_status)
253                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
254                         /* Fall through to update attibutes. */
255                 } else if (it->it_op & (IT_RENAME | IT_LINK)) {
256                         /* For rename, we want the source lookup to succeed */
257                         if (it->it_status) {
258                                 it->it_data = NULL;
259                                 GOTO(drop_req, rc = it->it_status);
260                         }
261                         it->it_data = dentry;
262                         /* Fall through to update attibutes. */
263                 } else if (it->it_op & (IT_UNLINK | IT_RMDIR)) {
264                         /* For remove ops, we want the lookup to succeed unless
265                          * the file truly doesn't exist */
266                         it->it_data = NULL;
267                         if (it->it_status == -ENOENT)
268                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
269                         /* No point in updating attributes that we're about to
270                          * unlink.  -phil */
271                         GOTO(out, flag = LL_LOOKUP_POSITIVE);
272                 } else if (it->it_op == IT_OPEN) {
273                         it->it_data = NULL;
274                         if (it->it_status && it->it_status != -EEXIST)
275                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
276                         /* Fall through to update attibutes. */
277                 } else if (it->it_op & (IT_RENAME2 | IT_LINK2)) {
278                         it->it_data = NULL;
279                         /* This means the target lookup is negative */
280                         if (mds_body->valid == 0)
281                                 GOTO(out, flag = LL_LOOKUP_NEGATIVE);
282                         /* XXX bug 289: should we maybe fall through here? -p */
283                         GOTO(out, flag = LL_LOOKUP_POSITIVE);
284                 }
285
286                 /* Do a getattr now that we have the lock */
287                 valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
288                 if (it->it_op == IT_READLINK) {
289                         valid |= OBD_MD_LINKNAME;
290                         symlen = mds_body->size;
291                 }
292                 ptlrpc_req_finished(request);
293                 request = NULL;
294                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
295                                  valid, symlen, &request);
296                 if (rc) {
297                         CERROR("failure %d inode "LPX64"\n", rc, ino);
298                         GOTO(drop_req, rc = -abs(rc));
299                 }
300                 offset = 0;
301         } else {
302                 struct ll_inode_info *lli = ll_i2info(parent);
303                 int mode;
304
305                 /* it_disposition == 0 indicates that it just did a simple lock
306                  * request, for which we are very thankful.  move along with
307                  * the local lookup then. */
308
309                 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
310                 offset = 0;
311
312                 ino = ll_inode_by_name(parent, dentry, &mode);
313                 if (!ino) {
314                         CERROR("inode %*s not found by name\n",
315                                dentry->d_name.len, dentry->d_name.name);
316                         GOTO(drop_lock, rc = -ENOENT);
317                 }
318
319                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode,
320                                  OBD_MD_FLNOTOBD|OBD_MD_FLEASIZE, 0, &request);
321                 if (rc) {
322                         CERROR("failure %d inode "LPX64"\n", rc, ino);
323                         GOTO(drop_req, rc = -abs(rc));
324                 }
325         }
326
327         EXIT;
328  out:
329         if (intent_finish != NULL) {
330                 rc = intent_finish(flag, request, de, it, offset, ino);
331                 dentry = *de; /* intent_finish may change *de */
332         } else {
333                 ptlrpc_req_finished(request);
334         }
335
336         if (it->it_op == IT_LOOKUP || rc < 0)
337                 ll_intent_release(dentry, it);
338
339         return rc;
340
341  drop_req:
342         ptlrpc_free_req(request);
343  drop_lock:
344 #warning FIXME: must release lock here
345         return rc;
346 }
347
348 /* Search "inode"'s alias list for a dentry that has the same name and parent as
349  * de.  If found, return it.  If not found, return de. */
350 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
351 {
352         struct list_head *tmp;
353
354         spin_lock(&dcache_lock);
355         list_for_each(tmp, &inode->i_dentry) {
356                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
357
358                 /* We are called here with 'de' already on the aliases list. */
359                 if (dentry == de)
360                         continue;
361
362                 if (dentry->d_parent != de->d_parent)
363                         continue;
364
365                 if (dentry->d_name.len != de->d_name.len)
366                         continue;
367
368                 if (memcmp(dentry->d_name.name, de->d_name.name,
369                            de->d_name.len) != 0)
370                         continue;
371
372                 spin_unlock(&dcache_lock);
373                 d_rehash(dentry);
374                 return dget(dentry);
375         }
376         spin_unlock(&dcache_lock);
377         return de;
378 }
379
380 static int
381 lookup2_finish(int flag, struct ptlrpc_request *request, struct dentry **de,
382                struct lookup_intent *it, int offset, obd_id ino)
383 {
384         struct dentry *dentry = *de, *saved = *de;
385         struct inode *inode = NULL;
386         struct ll_read_inode2_cookie lic;
387
388         if (flag == LL_LOOKUP_POSITIVE) {
389                 ENTRY;
390                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
391
392                 if (S_ISREG(lic.lic_body->mode) &&
393                     lic.lic_body->valid & OBD_MD_FLEASIZE) {
394                         LASSERT(request->rq_repmsg->bufcount > offset);
395                         lic.lic_lmm = lustre_msg_buf(request->rq_repmsg,
396                                                      offset + 1);
397                 } else {
398                         lic.lic_lmm = NULL;
399                 }
400
401                 /* No rpc's happen during iget4, -ENOMEM's are possible */
402                 LASSERT(ino != 0);
403                 inode = ll_iget(dentry->d_sb, ino, &lic);
404                 if (!inode) {
405                         /* XXX make sure that request is freed in this case;
406                          * I think it is, but double-check refcounts. -phil */
407                         RETURN(-ENOMEM);
408                 }
409
410                 dentry = *de = ll_find_alias(inode, dentry);
411
412                 /* We asked for a lock on the directory, and may have been
413                  * granted a lock on the inode.  Just in case, fixup the data
414                  * pointer. */
415                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
416                                    inode, sizeof(*inode));
417
418                 EXIT;
419         } else {
420                 ENTRY;
421         }
422
423         ptlrpc_req_finished(request);
424
425         dentry->d_op = &ll_d_ops;
426         if (ll_d2d(dentry) == NULL) {
427                 ll_set_dd(dentry);
428         }
429
430         if (dentry == saved)
431                 d_add(dentry, inode);
432
433         if (it->it_status == 0) {
434                 LL_SAVE_INTENT(dentry, it);
435         } else {
436                 dentry->d_it = NULL;
437                 CDEBUG(D_DENTRY,
438                        "D_IT dentry %p fsdata %p intent: %s status %d\n",
439                        dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
440                        it->it_status);
441         }
442
443         RETURN(0);
444 }
445
446 static struct dentry *ll_lookup2(struct inode *parent, struct dentry *dentry,
447                                  struct lookup_intent *it)
448 {
449         struct dentry *save = dentry;
450         int rc;
451
452         rc = ll_intent_lock(parent, &dentry, it, lookup2_finish);
453         if (rc < 0) {
454                 CERROR("ll_intent_lock: %d\n", rc);
455                 return ERR_PTR(rc);
456         }
457
458         if (dentry == save)
459                 return NULL;
460         else
461                 return dentry;
462 }
463
464 static struct inode *ll_create_node(struct inode *dir, const char *name,
465                                     int namelen, const char *tgt, int tgtlen,
466                                     int mode, __u64 extra,
467                                     struct lookup_intent *it,
468                                     struct lov_stripe_md *lsm)
469 {
470         struct inode *inode;
471         struct ptlrpc_request *request = NULL;
472         struct mds_body *body;
473         time_t time = CURRENT_TIME;
474         struct ll_sb_info *sbi = ll_i2sbi(dir);
475         struct ll_read_inode2_cookie lic;
476         struct lov_mds_md *lmm = NULL;
477         ENTRY;
478
479         if (it && it->it_disposition) {
480                 int rc = it->it_status;
481                 if (rc) {
482                         CERROR("error creating MDS inode for %*s: rc = %d\n",
483                                namelen, name, rc);
484                         RETURN(ERR_PTR(rc));
485                 }
486                 ll_invalidate_inode_pages(dir);
487                 request = it->it_data;
488                 body = lustre_msg_buf(request->rq_repmsg, 1);
489                 lic.lic_lmm = NULL;
490         } else {
491                 int gid = current->fsgid;
492                 int rc;
493
494                 if (lsm) {
495                         OBD_ALLOC(lmm, lsm->lsm_mds_easize);
496                         if (!lmm)
497                                 RETURN(ERR_PTR(-ENOMEM));
498                         lov_packmd(lmm, lsm);
499                         lic.lic_lmm = lmm;
500                 } else
501                         lic.lic_lmm = NULL;
502
503                 if (dir->i_mode & S_ISGID) {
504                         gid = dir->i_gid;
505                         if (S_ISDIR(mode))
506                                 mode |= S_ISGID;
507                 }
508
509                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
510                                 tgtlen, mode, current->fsuid, gid,
511                                 time, extra, lsm, &request);
512                 if (rc) {
513                         inode = ERR_PTR(rc);
514                         GOTO(out, rc);
515                 }
516                 body = lustre_msg_buf(request->rq_repmsg, 0);
517         }
518
519         lic.lic_body = body;
520
521         LASSERT(body->ino != 0);
522         inode = ll_iget(dir->i_sb, body->ino, &lic);
523         if (IS_ERR(inode)) {
524                 int rc = PTR_ERR(inode);
525                 CERROR("new_inode -fatal: rc %d\n", rc);
526                 LBUG();
527                 GOTO(out, rc);
528         }
529
530         if (!list_empty(&inode->i_dentry)) {
531                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
532                        body->ino, atomic_read(&inode->i_count),
533                        inode->i_nlink);
534                 iput(inode);
535                 LBUG();
536                 inode = ERR_PTR(-EIO);
537                 GOTO(out, -EIO);
538         }
539
540         if (it && it->it_disposition) {
541                 /* We asked for a lock on the directory, but were
542                  * granted a lock on the inode.  Since we finally have
543                  * an inode pointer, stuff it in the lock. */
544                 ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
545                                    inode, sizeof(*inode));
546         }
547
548         EXIT;
549  out:
550         if (lsm && lmm)
551                 OBD_FREE(lmm, lsm->lsm_mds_easize);
552         ptlrpc_req_finished(request);
553         return inode;
554 }
555
556 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
557                          const char *name, int len)
558 {
559         struct ptlrpc_request *request = NULL;
560         struct ll_sb_info *sbi = ll_i2sbi(dir);
561         int err;
562
563         ENTRY;
564
565         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child, mode, name, len,
566                          &request);
567         ptlrpc_req_finished(request);
568
569         RETURN(err);
570 }
571
572 int ll_mdc_link(struct dentry *src, struct inode *dir,
573                 const char *name, int len)
574 {
575         struct ptlrpc_request *request = NULL;
576         int err;
577         struct ll_sb_info *sbi = ll_i2sbi(dir);
578
579         ENTRY;
580
581         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
582         ptlrpc_req_finished(request);
583
584         RETURN(err);
585 }
586
587 int ll_mdc_rename(struct inode *src, struct inode *tgt,
588                   struct dentry *old, struct dentry *new)
589 {
590         struct ptlrpc_request *request = NULL;
591         struct ll_sb_info *sbi = ll_i2sbi(src);
592         int err;
593
594         ENTRY;
595
596         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
597                          old->d_name.name, old->d_name.len,
598                          new->d_name.name, new->d_name.len, &request);
599         ptlrpc_req_finished(request);
600
601         RETURN(err);
602 }
603
604 /*
605  * By the time this is called, we already have created the directory cache
606  * entry for the new file, but it is so far negative - it has no inode.
607  * We defer creating the OBD object(s) until open, to keep the intent and
608  * non-intent code paths similar, and also because we do not have the MDS
609  * inode number before calling ll_create_node() (which is needed for LOV),
610  * so we would need to do yet another RPC to the MDS to store the LOV EA
611  * data on the MDS.
612  *
613  * If the create succeeds, we fill in the inode information
614  * with d_instantiate().
615  */
616 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
617 {
618         struct lookup_intent *it;
619         struct inode *inode;
620         int rc = 0;
621         ENTRY;
622
623         LL_GET_INTENT(dentry, it);
624
625         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
626                                NULL, 0, mode, 0, it, NULL);
627
628         if (IS_ERR(inode))
629                 RETURN(PTR_ERR(inode));
630
631         if (it->it_disposition) {
632                 struct ll_inode_info *lli = ll_i2info(inode);
633                 memcpy(&lli->lli_intent_lock_handle, it->it_lock_handle,
634                        sizeof(lli->lli_intent_lock_handle));
635                 d_instantiate(dentry, inode);
636         } else {
637                 /* no directory data updates when intents rule */
638                 rc = ext2_add_nondir(dentry, inode);
639         }
640
641         RETURN(rc);
642 }
643
644 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
645                     int rdev)
646 {
647         struct lookup_intent *it;
648         struct inode *inode;
649         int rc = 0;
650
651         LL_GET_INTENT(dentry, it);
652
653         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
654                                NULL, 0, mode, rdev, it, NULL);
655
656         if (IS_ERR(inode))
657                 RETURN(PTR_ERR(inode));
658
659         /* no directory data updates when intents rule */
660         if (it && it->it_disposition)
661                 d_instantiate(dentry, inode);
662         else
663                 rc = ext2_add_nondir(dentry, inode);
664
665         return rc;
666 }
667
668 static int ll_symlink(struct inode *dir, struct dentry *dentry,
669                       const char *symname)
670 {
671         struct lookup_intent *it;
672         unsigned l = strlen(symname);
673         struct inode *inode;
674         struct ll_inode_info *lli;
675         int err = 0;
676         ENTRY;
677
678         LL_GET_INTENT(dentry, it);
679
680         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
681                                symname, l, S_IFLNK | S_IRWXUGO, 0, it, NULL);
682         if (IS_ERR(inode))
683                 RETURN(PTR_ERR(inode));
684
685         lli = ll_i2info(inode);
686
687         OBD_ALLOC(lli->lli_symlink_name, l + 1);
688         /* this _could_ be a non-fatal error, since the symlink is already
689          * stored on the MDS by this point, and we can re-get it in readlink.
690          */
691         if (!lli->lli_symlink_name)
692                 RETURN(-ENOMEM);
693
694         memcpy(lli->lli_symlink_name, symname, l + 1);
695         inode->i_size = l;
696
697         /* no directory data updates when intents rule */
698         if (it && it->it_disposition)
699                 d_instantiate(dentry, inode);
700         else
701                 err = ext2_add_nondir(dentry, inode);
702
703         RETURN(err);
704 }
705
706 static int ll_link(struct dentry *old_dentry, struct inode * dir,
707                    struct dentry *dentry)
708 {
709         struct lookup_intent *it;
710         struct inode *inode = old_dentry->d_inode;
711         int rc;
712
713         LL_GET_INTENT(dentry, it);
714
715         if (it && it->it_disposition) {
716                 if (it->it_status)
717                         RETURN(it->it_status);
718                 inode->i_ctime = CURRENT_TIME;
719                 ext2_inc_count(inode);
720                 atomic_inc(&inode->i_count);
721                 d_instantiate(dentry, inode);
722                 ll_invalidate_inode_pages(dir);
723                 RETURN(0);
724         }
725
726         if (S_ISDIR(inode->i_mode))
727                 return -EPERM;
728
729         if (inode->i_nlink >= EXT2_LINK_MAX)
730                 return -EMLINK;
731
732         rc = ll_mdc_link(old_dentry, dir,
733                           dentry->d_name.name, dentry->d_name.len);
734         if (rc)
735                 RETURN(rc);
736
737         inode->i_ctime = CURRENT_TIME;
738         ext2_inc_count(inode);
739         atomic_inc(&inode->i_count);
740
741         return ext2_add_nondir(dentry, inode);
742 }
743
744 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
745 {
746         struct lookup_intent *it;
747         struct inode * inode;
748         int err = -EMLINK;
749         ENTRY;
750
751         LL_GET_INTENT(dentry, it);
752
753         if (dir->i_nlink >= EXT2_LINK_MAX)
754                 goto out;
755
756         ext2_inc_count(dir);
757         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
758                                NULL, 0, S_IFDIR | mode, 0, it, NULL);
759         err = PTR_ERR(inode);
760         if (IS_ERR(inode))
761                 goto out_dir;
762
763         ext2_inc_count(inode);
764
765         err = ext2_make_empty(inode, dir);
766         if (err)
767                 goto out_fail;
768
769         /* no directory data updates when intents rule */
770         if (!it || !it->it_disposition) {
771                 err = ll_add_link(dentry, inode);
772                 if (err)
773                         goto out_fail;
774         }
775
776         d_instantiate(dentry, inode);
777 out:
778         EXIT;
779         return err;
780
781 out_fail:
782         ext2_dec_count(inode);
783         ext2_dec_count(inode);
784         iput(inode);
785         EXIT;
786 out_dir:
787         ext2_dec_count(dir);
788         EXIT;
789         goto out;
790 }
791
792 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
793                             struct lookup_intent *it, __u32 mode)
794 {
795         struct inode *inode = dentry->d_inode;
796         struct ext2_dir_entry_2 * de;
797         struct page * page;
798         int rc = 0;
799
800         if (it && it->it_disposition) {
801                 rc = it->it_status;
802                 ll_invalidate_inode_pages(dir);
803                 if (rc)
804                         GOTO(out, rc);
805                 GOTO(out_dec, 0);
806         }
807
808         de = ext2_find_entry(dir, dentry, &page);
809         if (!de)
810                 GOTO(out, rc = -ENOENT);
811         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
812                            dentry->d_name.name, dentry->d_name.len);
813         if (rc)
814                 GOTO(out, rc);
815
816         rc = ext2_delete_entry(de, page);
817         if (rc)
818                 GOTO(out, rc);
819
820         /* AED: not sure if needed - directory lock revocation should do it
821          * in the case where the client has cached it for non-intent ops.
822          */
823         ll_invalidate_inode_pages(dir);
824
825         inode->i_ctime = dir->i_ctime;
826 out_dec:
827         ext2_dec_count(inode);
828 out:
829         return rc;
830 }
831
832 static int ll_unlink(struct inode *dir, struct dentry *dentry)
833 {
834         struct lookup_intent * it;
835
836         LL_GET_INTENT(dentry, it);
837
838         return ll_common_unlink(dir, dentry, it, S_IFREG);
839 }
840
841 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
842 {
843         struct inode * inode = dentry->d_inode;
844         struct lookup_intent *it;
845         int rc;
846         ENTRY;
847
848         LL_GET_INTENT(dentry, it);
849
850         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
851                 RETURN(-ENOTEMPTY);
852
853         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
854         if (!rc) {
855                 inode->i_size = 0;
856                 ext2_dec_count(inode);
857                 ext2_dec_count(dir);
858         }
859
860         RETURN(rc);
861 }
862
863 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
864                      struct inode * new_dir, struct dentry * new_dentry)
865 {
866         struct lookup_intent *it;
867         struct inode * old_inode = old_dentry->d_inode;
868         struct inode * tgt_inode = new_dentry->d_inode;
869         struct page * dir_page = NULL;
870         struct ext2_dir_entry_2 * dir_de = NULL;
871         struct ext2_dir_entry_2 * old_de;
872         struct page * old_page;
873         int err;
874
875         LL_GET_INTENT(new_dentry, it);
876
877         if (it && it->it_disposition) {
878                 if (tgt_inode) {
879                         tgt_inode->i_ctime = CURRENT_TIME;
880                         tgt_inode->i_nlink--;
881                 }
882                 ll_invalidate_inode_pages(old_dir);
883                 ll_invalidate_inode_pages(new_dir);
884                 GOTO(out, err = it->it_status);
885         }
886
887         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
888         if (err)
889                 goto out;
890
891         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
892         if (!old_de)
893                 goto out;
894
895         if (S_ISDIR(old_inode->i_mode)) {
896                 err = -EIO;
897                 dir_de = ext2_dotdot(old_inode, &dir_page);
898                 if (!dir_de)
899                         goto out_old;
900         }
901
902         if (tgt_inode) {
903                 struct page *new_page;
904                 struct ext2_dir_entry_2 *new_de;
905
906                 err = -ENOTEMPTY;
907                 if (dir_de && !ext2_empty_dir (tgt_inode))
908                         goto out_dir;
909
910                 err = -ENOENT;
911                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
912                 if (!new_de)
913                         goto out_dir;
914                 ext2_inc_count(old_inode);
915                 ext2_set_link(new_dir, new_de, new_page, old_inode);
916                 tgt_inode->i_ctime = CURRENT_TIME;
917                 if (dir_de)
918                         tgt_inode->i_nlink--;
919                 ext2_dec_count(tgt_inode);
920         } else {
921                 if (dir_de) {
922                         err = -EMLINK;
923                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
924                                 goto out_dir;
925                 }
926                 ext2_inc_count(old_inode);
927                 err = ll_add_link(new_dentry, old_inode);
928                 if (err) {
929                         ext2_dec_count(old_inode);
930                         goto out_dir;
931                 }
932                 if (dir_de)
933                         ext2_inc_count(new_dir);
934         }
935
936         ext2_delete_entry (old_de, old_page);
937         ext2_dec_count(old_inode);
938
939         if (dir_de) {
940                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
941                 ext2_dec_count(old_dir);
942         }
943         return 0;
944
945 out_dir:
946         if (dir_de) {
947                 kunmap(dir_page);
948                 page_cache_release(dir_page);
949         }
950 out_old:
951         kunmap(old_page);
952         page_cache_release(old_page);
953 out:
954         return err;
955 }
956
957 struct inode_operations ll_dir_inode_operations = {
958         create:         ll_create,
959         lookup2:        ll_lookup2,
960         link:           ll_link,
961         unlink:         ll_unlink,
962         symlink:        ll_symlink,
963         mkdir:          ll_mkdir,
964         rmdir:          ll_rmdir,
965         mknod:          ll_mknod,
966         rename:         ll_rename,
967         setattr:        ll_setattr
968 };