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