Whamcloud - gitweb
Merge b_md into HEAD
[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  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  *  derived in small part from linux/fs/ext2/namei.c
22  *
23  *  Copyright (C) 1991, 1992  Linus Torvalds
24  *
25  *  Big-endian to little-endian byte-swapping/bitmaps by
26  *        David S. Miller (davem@caip.rutgers.edu), 1995
27  *  Directory entry file type support and forward compatibility hooks
28  *      for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
29  */
30
31 #include <linux/fs.h>
32 #include <linux/sched.h>
33 #include <linux/mm.h>
34 #include <linux/smp_lock.h>
35 #include <linux/quotaops.h>
36 #include <linux/highmem.h>
37 #include <linux/pagemap.h>
38
39 #define DEBUG_SUBSYSTEM S_LLITE
40
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lite.h>
43 #include <linux/lustre_dlm.h>
44
45 /* from dcache.c */
46 extern void ll_set_dd(struct dentry *de);
47
48 /* from super.c */
49 extern void ll_change_inode(struct inode *inode);
50 extern int ll_setattr(struct dentry *de, struct iattr *attr);
51
52 /* from dir.c */
53 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
54 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
55 int ext2_make_empty(struct inode *inode, struct inode *parent);
56 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
57                    struct dentry *dentry, struct page ** res_page);
58 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
59 int ext2_empty_dir (struct inode * inode);
60 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
61 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
62                    struct page *page, struct inode *inode);
63
64 /*
65  * Couple of helper functions - make the code slightly cleaner.
66  */
67 static inline void ext2_inc_count(struct inode *inode)
68 {
69         inode->i_nlink++;
70 }
71
72 /* postpone the disk update until the inode really goes away */
73 static inline void ext2_dec_count(struct inode *inode)
74 {
75         inode->i_nlink--;
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, lic->lic_lmm);
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         LASSERT(hash != 0);
129         inode = iget5_locked(sb, hash, ll_test_inode, ll_read_inode2, lic);
130
131         if (!inode)
132                 return ERR_PTR(-ENOMEM);
133
134         if (inode->i_state & I_NEW)
135                 unlock_new_inode(inode);
136
137         // XXX Coda always fills inodes, should Lustre?
138         return inode;
139 }
140 #else
141 struct inode *ll_iget(struct super_block *sb, ino_t hash,
142                       struct ll_read_inode2_cookie *lic)
143 {
144         struct inode *inode;
145         LASSERT(hash != 0);
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_SETATTR))
155                 return LCK_PW;
156         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP))
157                 return LCK_PR;
158
159         LBUG();
160         RETURN(-EINVAL);
161 }
162
163 int ll_it_open_error(int phase, struct lookup_intent *it)
164 {
165         if (it->it_disposition & IT_OPEN_OPEN) {
166                 if (phase == IT_OPEN_OPEN)
167                         return it->it_status;
168                 else
169                         return 0;
170         }
171
172         if (it->it_disposition & IT_OPEN_CREATE) {
173                 if (phase == IT_OPEN_CREATE)
174                         return it->it_status;
175                 else
176                         return 0;
177         }
178
179         if (it->it_disposition & IT_OPEN_LOOKUP) {
180                 if (phase == IT_OPEN_LOOKUP)
181                         return it->it_status;
182                 else
183                         return 0;
184         }
185         LBUG();
186         return 0;
187 }
188
189 #define IT_ENQ_COMPLETE (1<<16)
190
191 int ll_intent_lock(struct inode *parent, struct dentry **de,
192                    struct lookup_intent *it, intent_finish_cb intent_finish)
193 {
194         struct dentry *dentry = *de;
195         struct ll_sb_info *sbi = ll_i2sbi(parent);
196         struct lustre_handle lockh;
197         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
198         struct ptlrpc_request *request = NULL;
199         char *data = NULL;
200         int rc = 0, datalen = 0, offset, flag = 0;
201         obd_id ino = 0;
202         ENTRY;
203
204         if (it == NULL)
205                 it = &lookup_it;
206
207         CDEBUG(D_INFO, "name: %*s, intent: %s\n", dentry->d_name.len,
208                dentry->d_name.name, ldlm_it2str(it->it_op));
209
210         if (dentry->d_name.len > EXT2_NAME_LEN)
211                 RETURN(-ENAMETOOLONG);
212
213         if (!(it->it_disposition & IT_ENQ_COMPLETE)) {
214                 rc = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_PLAIN, it,
215                                  ll_intent_to_lock_mode(it), parent, dentry,
216                                  &lockh, data, datalen, parent,
217                                  sizeof(*parent));
218                 if (rc < 0)
219                         RETURN(rc);
220                 memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
221         }
222
223         request = (struct ptlrpc_request *)it->it_data;
224
225         /* non-zero it_disposition indicates that the server performed the
226          * intent on our behalf. */
227         if (it->it_disposition) {
228                 struct mds_body *mds_body;
229                 int mode;
230
231                 /* This long block is all about fixing up the local
232                  * state so that it is correct as of the moment
233                  * _before_ the operation was applied; that way, the
234                  * VFS will think that everything is normal and call
235                  * Lustre's regular FS function.
236                  *
237                  * If we're performing a creation, that means that unless the
238                  * creation failed with EEXIST, we should fake up a negative
239                  * dentry.  Likewise for the target of a hard link.
240                  *
241                  * For everything else, we want to lookup to succeed. */
242
243                 /* One additional note: if CREATE/MKDIR/etc succeeded,
244                  * we add an extra reference to the request because we
245                  * need to keep it around until ll_create gets called.
246                  * For anything else which results in
247                  * LL_LOOKUP_POSITIVE, we can do the iget()
248                  * immediately with the contents of the reply (in the
249                  * intent_finish callback).  In the create case,
250                  * however, we need to wait until ll_create_node to do
251                  * the iget() or the VFS will abort with -EEXISTS.
252                  */
253
254                 offset = 1;
255                 mds_body = lustre_msg_buf(request->rq_repmsg, offset);
256                 ino = mds_body->fid1.id;
257                 mode = mds_body->mode;
258
259                 /*We were called from revalidate2: did we find the same inode?*/
260                 if ((*de)->d_inode &&
261                     (ino != (*de)->d_inode->i_ino || 
262                    mds_body->fid1.generation != (*de)->d_inode->i_generation)) {
263                         it->it_disposition |= IT_ENQ_COMPLETE;
264                         RETURN(-ESTALE);
265                 }
266
267                 /* If we're doing an IT_OPEN which did not result in an actual
268                  * successful open, then we need to remove the bit which saves
269                  * this request for unconditional replay. */
270                 if (it->it_op & IT_OPEN &&
271                     (!(it->it_disposition & IT_OPEN_OPEN) ||
272                      it->it_status != 0))
273                         request->rq_flags &= ~PTL_RPC_FL_REPLAY;
274
275                 if (it->it_op & IT_CREAT) {
276                         mdc_store_inode_generation(request, 2, 1);
277                         /* The server will return to us, in it_disposition, an
278                          * indication of exactly what it_status refers to.
279                          *
280                          * If IT_OPEN_OPEN is set, then it_status refers to the
281                          * open() call, otherwise if IT_OPEN_CREATE is set, then
282                          * it status is the creation failure mode.  In either
283                          * case, one of IT_OPEN_NEG or IT_OPEN_POS will be set,
284                          * indicating whether the child lookup was successful.
285                          *
286                          * Else, if IT_OPEN_LOOKUP then it_status is the rc
287                          * of the child lookup.
288                          *
289                          * Finally, if none of the bits are set, then the
290                          * failure occurred while looking up the parent. */
291                         rc = ll_it_open_error(IT_OPEN_LOOKUP, it);
292                         if (rc)
293                                 GOTO(drop_req, rc);
294
295                         if (it->it_disposition & IT_OPEN_CREATE)
296                                 ptlrpc_request_addref(request);
297
298                         if (it->it_disposition & IT_OPEN_NEG)
299                                 flag = LL_LOOKUP_NEGATIVE;
300                         else
301                                 flag = LL_LOOKUP_POSITIVE;
302                 } else if (it->it_op == IT_OPEN) {
303                         LASSERT(!(it->it_disposition & IT_OPEN_CREATE));
304
305                         rc = ll_it_open_error(IT_OPEN_LOOKUP, it);
306                         if (rc)
307                                 GOTO(drop_req, rc);
308
309                         if (it->it_disposition & IT_OPEN_OPEN)
310                                 ptlrpc_request_addref(request);
311
312                         if (it->it_disposition & IT_OPEN_NEG)
313                                 flag = LL_LOOKUP_NEGATIVE;
314                         else
315                                 flag = LL_LOOKUP_POSITIVE;
316                 } else if (it->it_op & (IT_GETATTR | IT_SETATTR | IT_LOOKUP |
317                                         IT_READLINK)) {
318                         /* For check ops, we want the lookup to succeed */
319                         it->it_data = NULL;
320                         if (it->it_status)
321                                 flag = LL_LOOKUP_NEGATIVE;
322                         else
323                                 flag = LL_LOOKUP_POSITIVE;
324                 } else
325                         LBUG();
326         } else {
327                 obd_flag valid;
328                 int mode;
329
330                 LBUG(); /* For the moment, no non-intent locks */
331
332                 /* it_disposition == 0 indicates that it just did a simple lock
333                  * request, for which we are very thankful.  move along with
334                  * the local lookup then. */
335
336                 //memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
337                 offset = 0;
338
339                 ino = ll_inode_by_name(parent, dentry, &mode);
340                 if (!ino) {
341                         CERROR("inode %*s not found by name\n",
342                                dentry->d_name.len, dentry->d_name.name);
343                         GOTO(drop_lock, rc = -ENOENT);
344                 }
345
346                 valid = OBD_MD_FLNOTOBD;
347
348                 if (S_ISREG(mode)) {
349                         datalen = obd_size_wiremd(&sbi->ll_osc_conn, NULL),
350                         valid |= OBD_MD_FLEASIZE;
351                 } else {
352                         valid |= OBD_MD_FLBLOCKS;
353                 }
354
355                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode, valid,
356                                  datalen, &request);
357                 if (rc) {
358                         CERROR("failure %d inode "LPX64"\n", rc, ino);
359                         GOTO(drop_req, rc = -abs(rc));
360                 }
361         }
362
363         if (intent_finish != NULL) {
364                 rc = intent_finish(flag, request, de, it, offset, ino);
365                 dentry = *de; /* intent_finish may change *de */
366         } else {
367                 ptlrpc_req_finished(request);
368         }
369
370         /* This places the intent in the dentry so that the vfs_xxx
371          * operation can lay its hands on it; but that is not always
372          * needed...  (we need to save it in the GETATTR case for the
373          * benefit of ll_inode_revalidate -phil) */
374         if (it->it_op & (IT_OPEN | IT_GETATTR))
375                 LL_SAVE_INTENT(dentry, it);
376         else
377                 CDEBUG(D_DENTRY,
378                        "D_IT dentry %p fsdata %p intent: %s status %d\n",
379                        dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
380                        it->it_status);
381
382         if (it->it_op == IT_LOOKUP)
383                 ll_intent_release(dentry, it);
384
385         RETURN(rc);
386
387  drop_req:
388         ptlrpc_req_finished(request);
389  drop_lock:
390 #warning FIXME: must release lock here
391         RETURN(rc);
392 }
393
394 /* Search "inode"'s alias list for a dentry that has the same name and parent as
395  * de.  If found, return it.  If not found, return de. */
396 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
397 {
398         struct list_head *tmp;
399
400         spin_lock(&dcache_lock);
401         list_for_each(tmp, &inode->i_dentry) {
402                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
403
404                 /* We are called here with 'de' already on the aliases list. */
405                 if (dentry == de) {
406                         CERROR("whoops\n");
407                         continue;
408                 }
409
410                 if (dentry->d_parent != de->d_parent)
411                         continue;
412
413                 if (dentry->d_name.len != de->d_name.len)
414                         continue;
415
416                 if (memcmp(dentry->d_name.name, de->d_name.name,
417                            de->d_name.len) != 0)
418                         continue;
419
420                 if (!list_empty(&dentry->d_lru))
421                         list_del_init(&dentry->d_lru);
422
423                 list_del_init(&dentry->d_hash);
424                 spin_unlock(&dcache_lock);
425                 d_rehash(dentry);
426                 atomic_inc(&dentry->d_count);
427                 iput(inode);
428                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
429                 return dentry;
430         }
431
432         spin_unlock(&dcache_lock);
433
434         return de;
435 }
436
437 static int
438 lookup2_finish(int flag, struct ptlrpc_request *request, struct dentry **de,
439                struct lookup_intent *it, int offset, obd_id ino)
440 {
441         struct dentry *dentry = *de, *saved = *de;
442         struct inode *inode = NULL;
443         struct ll_read_inode2_cookie lic = {.lic_body = NULL, .lic_lmm = NULL};
444
445         if (!(flag & LL_LOOKUP_NEGATIVE)) {
446                 ENTRY;
447                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
448
449                 if (S_ISREG(lic.lic_body->mode) &&
450                     lic.lic_body->valid & OBD_MD_FLEASIZE) {
451                         LASSERT(request->rq_repmsg->bufcount > offset);
452                         lic.lic_lmm = lustre_msg_buf(request->rq_repmsg,
453                                                      offset + 1);
454                 } else {
455                         lic.lic_lmm = NULL;
456                 }
457
458                 /* No rpc's happen during iget4, -ENOMEM's are possible */
459                 inode = ll_iget(dentry->d_sb, ino, &lic);
460                 if (!inode) {
461                         /* XXX make sure that request is freed in this case;
462                          * I think it is, but double-check refcounts. -phil */
463                         RETURN(-ENOMEM);
464                 }
465
466                 dentry = *de = ll_find_alias(inode, dentry);
467
468                 /* We asked for a lock on the directory, and may have been
469                  * granted a lock on the inode.  Just in case, fixup the data
470                  * pointer. */
471                 mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
472                                    inode);
473         } else {
474                 ENTRY;
475         }
476
477         ptlrpc_req_finished(request);
478
479         dentry->d_op = &ll_d_ops;
480         ll_set_dd(dentry);
481
482         if (dentry == saved)
483                 d_add(dentry, inode);
484
485         RETURN(0);
486 }
487
488 static struct dentry *ll_lookup2(struct inode *parent, struct dentry *dentry,
489                                  struct lookup_intent *it)
490 {
491         struct dentry *save = dentry;
492         int rc;
493         ENTRY;
494
495         if (it && it->it_op == IT_TRUNC)
496                 it->it_op = IT_SETATTR;
497
498         rc = ll_intent_lock(parent, &dentry, it, lookup2_finish);
499         if (rc < 0) {
500                 CDEBUG(D_INFO, "ll_intent_lock: %d\n", rc);
501                 RETURN(ERR_PTR(rc));
502         }
503
504         if (dentry == save)
505                 RETURN(NULL);
506         else
507                 RETURN(dentry);
508 }
509
510 /* We depend on "mode" being set with the proper file type/umask by now */
511 static struct inode *ll_create_node(struct inode *dir, const char *name,
512                                     int namelen, const void *data, int datalen,
513                                     int mode, __u64 extra,
514                                     struct lookup_intent *it)
515 {
516         struct inode *inode;
517         struct ptlrpc_request *request = NULL;
518         struct mds_body *body;
519         time_t time = CURRENT_TIME;
520         struct ll_sb_info *sbi = ll_i2sbi(dir);
521         struct ll_read_inode2_cookie lic = { .lic_lmm = NULL, };
522         ENTRY;
523
524         if (it && it->it_disposition) {
525                 ll_invalidate_inode_pages(dir);
526                 request = it->it_data;
527                 body = lustre_msg_buf(request->rq_repmsg, 1);
528         } else {
529                 int gid = current->fsgid;
530                 int rc;
531
532                 if (dir->i_mode & S_ISGID) {
533                         gid = dir->i_gid;
534                         if (S_ISDIR(mode))
535                                 mode |= S_ISGID;
536                 }
537
538                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen,
539                                 data, datalen, mode, current->fsuid, gid,
540                                 time, extra, &request);
541                 if (rc) {
542                         inode = ERR_PTR(rc);
543                         GOTO(out, rc);
544                 }
545                 body = lustre_msg_buf(request->rq_repmsg, 0);
546         }
547
548         lic.lic_body = body;
549
550         inode = ll_iget(dir->i_sb, body->ino, &lic);
551         if (IS_ERR(inode)) {
552                 int rc = PTR_ERR(inode);
553                 CERROR("new_inode -fatal: rc %d\n", rc);
554                 LBUG();
555                 GOTO(out, rc);
556         }
557
558         if (!list_empty(&inode->i_dentry)) {
559                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
560                        body->ino, atomic_read(&inode->i_count),
561                        inode->i_nlink);
562                 iput(inode);
563                 LBUG();
564                 inode = ERR_PTR(-EIO);
565                 GOTO(out, -EIO);
566         }
567
568         if (it && it->it_disposition) {
569                 /* We asked for a lock on the directory, but were
570                  * granted a lock on the inode.  Since we finally have
571                  * an inode pointer, stuff it in the lock. */
572                 mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
573                                    inode);
574         }
575
576         EXIT;
577  out:
578         ptlrpc_req_finished(request);
579         return inode;
580 }
581
582 static int ll_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
583                          const char *name, int len)
584 {
585         struct ptlrpc_request *request = NULL;
586         struct ll_sb_info *sbi = ll_i2sbi(dir);
587         struct mds_body *body;
588         struct lov_stripe_md *lsm = NULL;
589         struct lustre_handle lockh;
590         struct lookup_intent it = { .it_op = IT_UNLINK };
591         struct obdo *oa;
592         int err;
593         struct mdc_unlink_data data;
594         ENTRY;
595
596         data.unl_dir = dir;
597         data.unl_de = child;
598         data.unl_mode = mode;
599         data.unl_name = name;
600         data.unl_len = len;
601
602         err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_PLAIN, &it, LCK_EX, dir,
603                          NULL, &lockh, NULL, 0, &data, sizeof(data));
604         mdc_put_rpc_lock(&mdc_rpc_lock, &it); 
605         request = (struct ptlrpc_request *)it.it_data;
606         if (err < 0)
607                 GOTO(out, err);
608         if (it.it_status)
609                 GOTO(out, err = it.it_status);
610         err = 0;
611
612         body = lustre_msg_buf(request->rq_repmsg, 1);
613         LASSERT(body != NULL);
614         if (!(body->valid & OBD_MD_FLEASIZE))
615                 GOTO(out, 0);
616
617         /* The MDS sent back the EA because we unlinked the last reference
618          * to this file.  Use this EA to unlink the objects on the OST */
619         err = obd_unpackmd(ll_i2obdconn(dir), &lsm,
620                            lustre_msg_buf(request->rq_repmsg, 2));
621         if (err < 0)
622                 CERROR("obd_unpackmd: %d\n", err);
623
624         oa = obdo_alloc();
625         if (oa == NULL)
626                 GOTO(out_unlock, err = -ENOMEM);
627
628         oa->o_id = lsm->lsm_object_id;
629         oa->o_mode = body->mode & S_IFMT;
630         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
631
632         err = obd_destroy(ll_i2obdconn(dir), oa, lsm, NULL);
633         obdo_free(oa);
634         if (err)
635                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
636                        lsm->lsm_object_id, err);
637
638         obd_free_memmd(ll_i2obdconn(dir), &lsm);
639  out_unlock:
640         ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
641  out:
642         ptlrpc_req_finished(request);
643         return err;
644 }
645
646 /*
647  * By the time this is called, we already have created the directory cache
648  * entry for the new file, but it is so far negative - it has no inode.
649  *
650  * We defer creating the OBD object(s) until open, to keep the intent and
651  * non-intent code paths similar, and also because we do not have the MDS
652  * inode number before calling ll_create_node() (which is needed for LOV),
653  * so we would need to do yet another RPC to the MDS to store the LOV EA
654  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
655  * lmm_size in datalen (the MDS still has code which will handle that).
656  *
657  * If the create succeeds, we fill in the inode information
658  * with d_instantiate().
659  */
660 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
661 {
662         struct lookup_intent *it;
663         struct inode *inode;
664         int rc = 0;
665         ENTRY;
666
667         it = dentry->d_it;
668
669         rc = ll_it_open_error(IT_OPEN_CREATE, it);
670         if (rc) {
671                 LL_GET_INTENT(dentry, it);
672                 ptlrpc_req_finished(it->it_data);
673                 RETURN(rc);
674         }
675
676         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
677                                NULL, 0, mode, 0, it);
678
679         if (IS_ERR(inode)) {
680                 LL_GET_INTENT(dentry, it);
681                 RETURN(PTR_ERR(inode));
682         }
683
684         /* no directory data updates when intents rule */
685         if (it && it->it_disposition) {
686                 d_instantiate(dentry, inode);
687                 RETURN(0);
688         }
689
690         rc = ext2_add_nondir(dentry, inode);
691         RETURN(rc);
692 }
693
694 static int ll_mknod2(struct inode *dir, const char *name, int len, int mode,
695                      int rdev)
696 {
697         struct ptlrpc_request *request = NULL;
698         time_t time = CURRENT_TIME;
699         struct ll_sb_info *sbi = ll_i2sbi(dir);
700         int err = -EMLINK;
701         ENTRY;
702
703         if (dir->i_nlink >= EXT2_LINK_MAX)
704                 RETURN(err);
705
706         mode &= ~current->fs->umask;
707
708         switch (mode & S_IFMT) {
709         case 0: case S_IFREG:
710                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
711         case S_IFCHR: case S_IFBLK:
712         case S_IFIFO: case S_IFSOCK:
713                 err = mdc_create(&sbi->ll_mdc_conn, dir, name, len, NULL, 0,
714                                  mode, current->fsuid, current->fsgid, time,
715                                  rdev, &request);
716                 ptlrpc_req_finished(request);
717                 break;
718         case S_IFDIR:
719                 err = -EPERM;
720                 break;
721         default:
722                 err = -EINVAL;
723         }
724         RETURN(err);
725 }
726
727 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
728                     int rdev)
729 {
730         struct lookup_intent *it;
731         struct inode *inode;
732         int rc = 0;
733
734         LL_GET_INTENT(dentry, it);
735
736         if ((mode & S_IFMT) == 0)
737                 mode |= S_IFREG;
738         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
739                                NULL, 0, mode, rdev, it);
740
741         if (IS_ERR(inode))
742                 RETURN(PTR_ERR(inode));
743
744         /* no directory data updates when intents rule */
745         if (it && it->it_disposition)
746                 d_instantiate(dentry, inode);
747         else
748                 rc = ext2_add_nondir(dentry, inode);
749
750         return rc;
751 }
752
753 static int ll_symlink2(struct inode *dir, const char *name, int len,
754                        const char *tgt)
755 {
756         struct ptlrpc_request *request = NULL;
757         time_t time = CURRENT_TIME;
758         struct ll_sb_info *sbi = ll_i2sbi(dir);
759         int err = -EMLINK;
760         ENTRY;
761
762         if (dir->i_nlink >= EXT2_LINK_MAX)
763                 RETURN(err);
764
765         err = mdc_create(&sbi->ll_mdc_conn, dir, name, len,
766                          tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
767                          current->fsuid, current->fsgid, time, 0, &request);
768         ptlrpc_req_finished(request);
769         RETURN(err);
770 }
771
772 static int ll_symlink(struct inode *dir, struct dentry *dentry,
773                       const char *symname)
774 {
775         struct lookup_intent *it;
776         unsigned l = strlen(symname) + 1;
777         struct inode *inode;
778         struct ll_inode_info *lli;
779         int err = 0;
780         ENTRY;
781
782         LL_GET_INTENT(dentry, it);
783
784         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
785                                symname, l, S_IFLNK | S_IRWXUGO, 0, it);
786         if (IS_ERR(inode))
787                 RETURN(PTR_ERR(inode));
788
789         lli = ll_i2info(inode);
790
791         OBD_ALLOC(lli->lli_symlink_name, l);
792         /* this _could_ be a non-fatal error, since the symlink is already
793          * stored on the MDS by this point, and we can re-get it in readlink.
794          */
795         if (!lli->lli_symlink_name)
796                 RETURN(-ENOMEM);
797
798         memcpy(lli->lli_symlink_name, symname, l);
799         inode->i_size = l - 1;
800
801         /* no directory data updates when intents rule */
802         if (it && it->it_disposition)
803                 d_instantiate(dentry, inode);
804         else
805                 err = ext2_add_nondir(dentry, inode);
806
807         RETURN(err);
808 }
809
810 static int ll_link2(struct inode *src, struct inode *dir,
811                     const char *name, int len)
812 {
813         struct ptlrpc_request *request = NULL;
814         int err;
815         struct ll_sb_info *sbi = ll_i2sbi(dir);
816
817         ENTRY;
818
819         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
820         ptlrpc_req_finished(request);
821
822         RETURN(err);
823 }
824
825 static int ll_link(struct dentry *old_dentry, struct inode * dir,
826                    struct dentry *dentry)
827 {
828         struct lookup_intent *it;
829         struct inode *inode = old_dentry->d_inode;
830         int rc;
831
832         LL_GET_INTENT(dentry, it);
833
834         if (it && it->it_disposition) {
835                 if (it->it_status)
836                         RETURN(it->it_status);
837                 inode->i_ctime = CURRENT_TIME;
838                 ext2_inc_count(inode);
839                 atomic_inc(&inode->i_count);
840                 d_instantiate(dentry, inode);
841                 ll_invalidate_inode_pages(dir);
842                 RETURN(0);
843         }
844
845         if (S_ISDIR(inode->i_mode))
846                 return -EPERM;
847
848         if (inode->i_nlink >= EXT2_LINK_MAX)
849                 return -EMLINK;
850
851         rc = ll_link2(old_dentry->d_inode, dir,
852                       dentry->d_name.name, dentry->d_name.len);
853         if (rc)
854                 RETURN(rc);
855
856         inode->i_ctime = CURRENT_TIME;
857         ext2_inc_count(inode);
858         atomic_inc(&inode->i_count);
859
860         return ext2_add_nondir(dentry, inode);
861 }
862
863 static int ll_mkdir2(struct inode *dir, const char *name, int len, int mode)
864 {
865         struct ptlrpc_request *request = NULL;
866         time_t time = CURRENT_TIME;
867         struct ll_sb_info *sbi = ll_i2sbi(dir);
868         int err = -EMLINK;
869         ENTRY;
870
871         if (dir->i_nlink >= EXT2_LINK_MAX)
872                 RETURN(err);
873
874         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
875         err = mdc_create(&sbi->ll_mdc_conn, dir, name, len, NULL, 0,
876                          mode, current->fsuid, current->fsgid,
877                          time, 0, &request);
878         ptlrpc_req_finished(request);
879         RETURN(err);
880 }
881
882
883 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
884 {
885         struct lookup_intent *it;
886         struct inode * inode;
887         int err = -EMLINK;
888         ENTRY;
889
890         LL_GET_INTENT(dentry, it);
891
892         if (dir->i_nlink >= EXT2_LINK_MAX)
893                 goto out;
894
895         ext2_inc_count(dir);
896         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
897                                NULL, 0, S_IFDIR | mode, 0, it);
898         err = PTR_ERR(inode);
899         if (IS_ERR(inode))
900                 goto out_dir;
901
902         err = ext2_make_empty(inode, dir);
903         if (err)
904                 goto out_fail;
905
906         /* no directory data updates when intents rule */
907         if (!it || !it->it_disposition) {
908                 /* XXX FIXME This code needs re-checked for non-intents */
909                 ext2_inc_count(inode);
910                 err = ll_add_link(dentry, inode);
911                 if (err)
912                         goto out_fail;
913         }
914
915         d_instantiate(dentry, inode);
916 out:
917         EXIT;
918         return err;
919
920 out_fail:
921         ext2_dec_count(inode);
922         ext2_dec_count(inode);
923         iput(inode);
924         EXIT;
925 out_dir:
926         ext2_dec_count(dir);
927         EXIT;
928         goto out;
929 }
930
931 static int ll_rmdir2(struct inode *dir, const char *name, int len)
932 {
933         int rc;
934         ENTRY;
935
936         rc = ll_mdc_unlink(dir, NULL, S_IFDIR, name, len);
937         RETURN(rc);
938 }
939
940 static int ll_unlink2(struct inode *dir, const char *name, int len)
941 {
942         int rc;
943         ENTRY;
944
945         rc = ll_mdc_unlink(dir, NULL, S_IFREG, name, len);
946         RETURN(rc);
947 }
948
949 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
950                             struct lookup_intent *it, __u32 mode)
951 {
952         struct inode *inode = dentry->d_inode;
953         struct ext2_dir_entry_2 * de;
954         struct page * page;
955         int rc = 0;
956         ENTRY;
957
958         if (it && it->it_disposition) {
959                 rc = it->it_status;
960                 ll_invalidate_inode_pages(dir);
961                 if (rc)
962                         GOTO(out, rc);
963                 GOTO(out_dec, 0);
964         }
965
966         de = ext2_find_entry(dir, dentry, &page);
967         if (!de)
968                 GOTO(out, rc = -ENOENT);
969         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
970                            dentry->d_name.name, dentry->d_name.len);
971         if (rc)
972                 GOTO(out, rc);
973
974         rc = ext2_delete_entry(de, page);
975         if (rc)
976                 GOTO(out, rc);
977
978         /* AED: not sure if needed - directory lock revocation should do it
979          * in the case where the client has cached it for non-intent ops.
980          */
981         ll_invalidate_inode_pages(dir);
982
983         inode->i_ctime = dir->i_ctime;
984         EXIT;
985 out_dec:
986         ext2_dec_count(inode);
987 out:
988         return rc;
989 }
990
991 static int ll_unlink(struct inode *dir, struct dentry *dentry)
992 {
993         struct lookup_intent * it;
994         ENTRY;
995
996         LL_GET_INTENT(dentry, it);
997
998         RETURN(ll_common_unlink(dir, dentry, it, S_IFREG));
999 }
1000
1001 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1002 {
1003         struct inode * inode = dentry->d_inode;
1004         struct lookup_intent *it;
1005         int rc;
1006         ENTRY;
1007
1008         LL_GET_INTENT(dentry, it);
1009
1010         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
1011                 RETURN(-ENOTEMPTY);
1012
1013         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
1014         if (!rc) {
1015                 inode->i_size = 0;
1016                 ext2_dec_count(inode);
1017                 ext2_dec_count(dir);
1018         }
1019
1020         RETURN(rc);
1021 }
1022
1023 static int ll_rename2(struct inode *src, struct inode *tgt,
1024                       const char *oldname, int oldlen,
1025                       const char *newname, int newlen)
1026 {
1027         struct ptlrpc_request *request = NULL;
1028         struct ll_sb_info *sbi = ll_i2sbi(src);
1029         int err;
1030         ENTRY;
1031
1032         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
1033                          oldname, oldlen, newname, newlen, &request);
1034         ptlrpc_req_finished(request);
1035
1036         RETURN(err);
1037 }
1038
1039
1040
1041 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
1042                      struct inode * new_dir, struct dentry * new_dentry)
1043 {
1044         struct lookup_intent *it;
1045         struct inode * old_inode = old_dentry->d_inode;
1046         struct inode * tgt_inode = new_dentry->d_inode;
1047         struct page * dir_page = NULL;
1048         struct ext2_dir_entry_2 * dir_de = NULL;
1049         struct ext2_dir_entry_2 * old_de;
1050         struct page * old_page;
1051         int err;
1052
1053         LL_GET_INTENT(new_dentry, it);
1054
1055         if (it && it->it_disposition) {
1056                 if (tgt_inode) {
1057                         tgt_inode->i_ctime = CURRENT_TIME;
1058                         tgt_inode->i_nlink--;
1059                 }
1060                 ll_invalidate_inode_pages(old_dir);
1061                 ll_invalidate_inode_pages(new_dir);
1062                 GOTO(out, err = it->it_status);
1063         }
1064
1065         err = ll_rename2(old_dir, new_dir,
1066                          old_dentry->d_name.name, old_dentry->d_name.len,
1067                          new_dentry->d_name.name, new_dentry->d_name.len);
1068         if (err)
1069                 goto out;
1070
1071         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
1072         if (!old_de)
1073                 goto out;
1074
1075         if (S_ISDIR(old_inode->i_mode)) {
1076                 err = -EIO;
1077                 dir_de = ext2_dotdot(old_inode, &dir_page);
1078                 if (!dir_de)
1079                         goto out_old;
1080         }
1081
1082         if (tgt_inode) {
1083                 struct page *new_page;
1084                 struct ext2_dir_entry_2 *new_de;
1085
1086                 err = -ENOTEMPTY;
1087                 if (dir_de && !ext2_empty_dir (tgt_inode))
1088                         goto out_dir;
1089
1090                 err = -ENOENT;
1091                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
1092                 if (!new_de)
1093                         goto out_dir;
1094                 ext2_inc_count(old_inode);
1095                 ext2_set_link(new_dir, new_de, new_page, old_inode);
1096                 tgt_inode->i_ctime = CURRENT_TIME;
1097                 if (dir_de)
1098                         tgt_inode->i_nlink--;
1099                 ext2_dec_count(tgt_inode);
1100         } else {
1101                 if (dir_de) {
1102                         err = -EMLINK;
1103                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
1104                                 goto out_dir;
1105                 }
1106                 ext2_inc_count(old_inode);
1107                 err = ll_add_link(new_dentry, old_inode);
1108                 if (err) {
1109                         ext2_dec_count(old_inode);
1110                         goto out_dir;
1111                 }
1112                 if (dir_de)
1113                         ext2_inc_count(new_dir);
1114         }
1115
1116         ext2_delete_entry (old_de, old_page);
1117         ext2_dec_count(old_inode);
1118
1119         if (dir_de) {
1120                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
1121                 ext2_dec_count(old_dir);
1122         }
1123         return 0;
1124
1125 out_dir:
1126         if (dir_de) {
1127                 kunmap(dir_page);
1128                 page_cache_release(dir_page);
1129         }
1130 out_old:
1131         kunmap(old_page);
1132         page_cache_release(old_page);
1133 out:
1134         return err;
1135 }
1136
1137 extern int ll_inode_revalidate(struct dentry *dentry);
1138 struct inode_operations ll_dir_inode_operations = {
1139         create:          ll_create,
1140         lookup2:         ll_lookup2,
1141         link:            ll_link,
1142         link2:           ll_link2,
1143         unlink:          ll_unlink,
1144         unlink2:         ll_unlink2,
1145         symlink:         ll_symlink,
1146         symlink2:        ll_symlink2,
1147         mkdir:           ll_mkdir,
1148         mkdir2:          ll_mkdir2,
1149         rmdir:           ll_rmdir,
1150         rmdir2:          ll_rmdir2,
1151         mknod:           ll_mknod,
1152         mknod2:          ll_mknod2,
1153         rename:          ll_rename,
1154         rename2:         ll_rename2,
1155         setattr:         ll_setattr,
1156         revalidate:      ll_inode_revalidate,
1157 };