Whamcloud - gitweb
file ext3-largefile.diff was initially added on branch b_devel.
[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                         /* For check ops, we want the lookup to succeed */
318                         it->it_data = NULL;
319                         if (it->it_status)
320                                 flag = LL_LOOKUP_NEGATIVE;
321                         else
322                                 flag = LL_LOOKUP_POSITIVE;
323                 } else
324                         LBUG();
325         } else {
326                 obd_flag valid;
327                 int mode;
328
329                 LBUG(); /* For the moment, no non-intent locks */
330
331                 /* it_disposition == 0 indicates that it just did a simple lock
332                  * request, for which we are very thankful.  move along with
333                  * the local lookup then. */
334
335                 //memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
336                 offset = 0;
337
338                 ino = ll_inode_by_name(parent, dentry, &mode);
339                 if (!ino) {
340                         CERROR("inode %*s not found by name\n",
341                                dentry->d_name.len, dentry->d_name.name);
342                         GOTO(drop_lock, rc = -ENOENT);
343                 }
344
345                 valid = OBD_MD_FLNOTOBD;
346
347                 if (S_ISREG(mode)) {
348                         datalen = obd_size_wiremd(&sbi->ll_osc_conn, NULL),
349                         valid |= OBD_MD_FLEASIZE;
350                 } else {
351                         valid |= OBD_MD_FLBLOCKS;
352                 }
353
354                 rc = mdc_getattr(&sbi->ll_mdc_conn, ino, mode, valid,
355                                  datalen, &request);
356                 if (rc) {
357                         CERROR("failure %d inode "LPX64"\n", rc, ino);
358                         GOTO(drop_req, rc = -abs(rc));
359                 }
360         }
361
362         if (intent_finish != NULL) {
363                 rc = intent_finish(flag, request, de, it, offset, ino);
364                 dentry = *de; /* intent_finish may change *de */
365         } else {
366                 ptlrpc_req_finished(request);
367         }
368
369         /* This places the intent in the dentry so that the vfs_xxx
370          * operation can lay its hands on it; but that is not always
371          * needed...  (we need to save it in the GETATTR case for the
372          * benefit of ll_inode_revalidate -phil) */
373         if (it->it_op & (IT_OPEN | IT_GETATTR))
374                 LL_SAVE_INTENT(dentry, it);
375         else
376                 CDEBUG(D_DENTRY,
377                        "D_IT dentry %p fsdata %p intent: %s status %d\n",
378                        dentry, ll_d2d(dentry), ldlm_it2str(it->it_op),
379                        it->it_status);
380
381         if (it->it_op == IT_LOOKUP)
382                 ll_intent_release(dentry, it);
383
384         RETURN(rc);
385
386  drop_req:
387         ptlrpc_req_finished(request);
388  drop_lock:
389 #warning FIXME: must release lock here
390         RETURN(rc);
391 }
392
393 /* Search "inode"'s alias list for a dentry that has the same name and parent as
394  * de.  If found, return it.  If not found, return de. */
395 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
396 {
397         struct list_head *tmp;
398
399         spin_lock(&dcache_lock);
400         list_for_each(tmp, &inode->i_dentry) {
401                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
402
403                 /* We are called here with 'de' already on the aliases list. */
404                 if (dentry == de) {
405                         CERROR("whoops\n");
406                         continue;
407                 }
408
409                 if (dentry->d_parent != de->d_parent)
410                         continue;
411
412                 if (dentry->d_name.len != de->d_name.len)
413                         continue;
414
415                 if (memcmp(dentry->d_name.name, de->d_name.name,
416                            de->d_name.len) != 0)
417                         continue;
418
419                 if (!list_empty(&dentry->d_lru))
420                         list_del_init(&dentry->d_lru);
421
422                 list_del_init(&dentry->d_hash);
423                 spin_unlock(&dcache_lock);
424                 d_rehash(dentry);
425                 atomic_inc(&dentry->d_count);
426                 iput(inode);
427                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
428                 return dentry;
429         }
430
431         spin_unlock(&dcache_lock);
432
433         return de;
434 }
435
436 static int
437 lookup2_finish(int flag, struct ptlrpc_request *request, struct dentry **de,
438                struct lookup_intent *it, int offset, obd_id ino)
439 {
440         struct dentry *dentry = *de, *saved = *de;
441         struct inode *inode = NULL;
442         struct ll_read_inode2_cookie lic = {.lic_body = NULL, .lic_lmm = NULL};
443
444         if (!(flag & LL_LOOKUP_NEGATIVE)) {
445                 ENTRY;
446                 lic.lic_body = lustre_msg_buf(request->rq_repmsg, offset);
447
448                 if (S_ISREG(lic.lic_body->mode) &&
449                     lic.lic_body->valid & OBD_MD_FLEASIZE) {
450                         LASSERT(request->rq_repmsg->bufcount > offset);
451                         lic.lic_lmm = lustre_msg_buf(request->rq_repmsg,
452                                                      offset + 1);
453                 } else {
454                         lic.lic_lmm = NULL;
455                 }
456
457                 /* No rpc's happen during iget4, -ENOMEM's are possible */
458                 inode = ll_iget(dentry->d_sb, ino, &lic);
459                 if (!inode) {
460                         /* XXX make sure that request is freed in this case;
461                          * I think it is, but double-check refcounts. -phil */
462                         RETURN(-ENOMEM);
463                 }
464
465                 dentry = *de = ll_find_alias(inode, dentry);
466
467                 /* We asked for a lock on the directory, and may have been
468                  * granted a lock on the inode.  Just in case, fixup the data
469                  * pointer. */
470                 mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
471                                    inode);
472         } else {
473                 ENTRY;
474         }
475
476         ptlrpc_req_finished(request);
477
478         dentry->d_op = &ll_d_ops;
479         ll_set_dd(dentry);
480
481         if (dentry == saved)
482                 d_add(dentry, inode);
483
484         RETURN(0);
485 }
486
487 static struct dentry *ll_lookup2(struct inode *parent, struct dentry *dentry,
488                                  struct lookup_intent *it)
489 {
490         struct dentry *save = dentry;
491         int rc;
492         ENTRY;
493
494         if (it && it->it_op == IT_TRUNC)
495                 it->it_op = IT_SETATTR;
496
497         rc = ll_intent_lock(parent, &dentry, it, lookup2_finish);
498         if (rc < 0) {
499                 CDEBUG(D_INFO, "ll_intent_lock: %d\n", rc);
500                 RETURN(ERR_PTR(rc));
501         }
502
503         if (dentry == save)
504                 RETURN(NULL);
505         else
506                 RETURN(dentry);
507 }
508
509 /* We depend on "mode" being set with the proper file type/umask by now */
510 static struct inode *ll_create_node(struct inode *dir, const char *name,
511                                     int namelen, const void *data, int datalen,
512                                     int mode, __u64 extra,
513                                     struct lookup_intent *it)
514 {
515         struct inode *inode;
516         struct ptlrpc_request *request = NULL;
517         struct mds_body *body;
518         time_t time = CURRENT_TIME;
519         struct ll_sb_info *sbi = ll_i2sbi(dir);
520         struct ll_read_inode2_cookie lic = { .lic_lmm = NULL, };
521         ENTRY;
522
523         if (it && it->it_disposition) {
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                 mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
572                                    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         struct mds_body *body;
587         struct lov_stripe_md *lsm = NULL;
588         struct lustre_handle lockh;
589         struct lookup_intent it = { .it_op = IT_UNLINK };
590         struct obdo *oa;
591         int err;
592         struct mdc_unlink_data data;
593         ENTRY;
594
595         data.unl_dir = dir;
596         data.unl_de = child;
597         data.unl_mode = mode;
598         data.unl_name = name;
599         data.unl_len = len;
600
601         err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_PLAIN, &it, LCK_EX, dir,
602                          NULL, &lockh, NULL, 0, &data, sizeof(data));
603         mdc_put_rpc_lock(&mdc_rpc_lock, &it); 
604         request = (struct ptlrpc_request *)it.it_data;
605         if (err < 0)
606                 GOTO(out, err);
607         if (it.it_status)
608                 GOTO(out, err = it.it_status);
609         err = 0;
610
611         body = lustre_msg_buf(request->rq_repmsg, 1);
612         LASSERT(body != NULL);
613         if (!(body->valid & OBD_MD_FLEASIZE))
614                 GOTO(out, 0);
615
616         /* The MDS sent back the EA because we unlinked the last reference
617          * to this file.  Use this EA to unlink the objects on the OST */
618         err = obd_unpackmd(ll_i2obdconn(dir), &lsm,
619                            lustre_msg_buf(request->rq_repmsg, 2));
620         if (err < 0)
621                 CERROR("obd_unpackmd: %d\n", err);
622
623         oa = obdo_alloc();
624         if (oa == NULL)
625                 GOTO(out_unlock, err = -ENOMEM);
626
627         oa->o_id = lsm->lsm_object_id;
628         oa->o_mode = body->mode & S_IFMT;
629         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
630
631         err = obd_destroy(ll_i2obdconn(dir), oa, lsm, NULL);
632         obdo_free(oa);
633         if (err)
634                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
635                        lsm->lsm_object_id, err);
636
637         obd_free_memmd(ll_i2obdconn(dir), &lsm);
638  out_unlock:
639         ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
640  out:
641         ptlrpc_req_finished(request);
642         return err;
643 }
644
645 /*
646  * By the time this is called, we already have created the directory cache
647  * entry for the new file, but it is so far negative - it has no inode.
648  *
649  * We defer creating the OBD object(s) until open, to keep the intent and
650  * non-intent code paths similar, and also because we do not have the MDS
651  * inode number before calling ll_create_node() (which is needed for LOV),
652  * so we would need to do yet another RPC to the MDS to store the LOV EA
653  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
654  * lmm_size in datalen (the MDS still has code which will handle that).
655  *
656  * If the create succeeds, we fill in the inode information
657  * with d_instantiate().
658  */
659 static int ll_create(struct inode *dir, struct dentry *dentry, int mode)
660 {
661         struct lookup_intent *it;
662         struct inode *inode;
663         int rc = 0;
664         ENTRY;
665
666         it = dentry->d_it;
667
668         rc = ll_it_open_error(IT_OPEN_CREATE, it);
669         if (rc) {
670                 LL_GET_INTENT(dentry, it);
671                 ptlrpc_req_finished(it->it_data);
672                 RETURN(rc);
673         }
674
675         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
676                                NULL, 0, mode, 0, it);
677
678         if (IS_ERR(inode)) {
679                 LL_GET_INTENT(dentry, it);
680                 RETURN(PTR_ERR(inode));
681         }
682
683         /* no directory data updates when intents rule */
684         if (it && it->it_disposition) {
685                 d_instantiate(dentry, inode);
686                 RETURN(0);
687         }
688
689         rc = ext2_add_nondir(dentry, inode);
690         RETURN(rc);
691 }
692
693 static int ll_mknod2(struct inode *dir, const char *name, int len, int mode,
694                      int rdev)
695 {
696         struct ptlrpc_request *request = NULL;
697         time_t time = CURRENT_TIME;
698         struct ll_sb_info *sbi = ll_i2sbi(dir);
699         int err = -EMLINK;
700         ENTRY;
701
702         if (dir->i_nlink >= EXT2_LINK_MAX)
703                 RETURN(err);
704
705         mode &= ~current->fs->umask;
706
707         switch (mode & S_IFMT) {
708         case 0: case S_IFREG:
709                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
710         case S_IFCHR: case S_IFBLK:
711         case S_IFIFO: case S_IFSOCK:
712                 err = mdc_create(&sbi->ll_mdc_conn, dir, name, len, NULL, 0,
713                                  mode, current->fsuid, current->fsgid, time,
714                                  rdev, &request);
715                 ptlrpc_req_finished(request);
716                 break;
717         case S_IFDIR:
718                 err = -EPERM;
719                 break;
720         default:
721                 err = -EINVAL;
722         }
723         RETURN(err);
724 }
725
726 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
727                     int rdev)
728 {
729         struct lookup_intent *it;
730         struct inode *inode;
731         int rc = 0;
732
733         LL_GET_INTENT(dentry, it);
734
735         if ((mode & S_IFMT) == 0)
736                 mode |= S_IFREG;
737         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
738                                NULL, 0, mode, rdev, it);
739
740         if (IS_ERR(inode))
741                 RETURN(PTR_ERR(inode));
742
743         /* no directory data updates when intents rule */
744         if (it && it->it_disposition)
745                 d_instantiate(dentry, inode);
746         else
747                 rc = ext2_add_nondir(dentry, inode);
748
749         return rc;
750 }
751
752 static int ll_symlink2(struct inode *dir, const char *name, int len,
753                        const char *tgt)
754 {
755         struct ptlrpc_request *request = NULL;
756         time_t time = CURRENT_TIME;
757         struct ll_sb_info *sbi = ll_i2sbi(dir);
758         int err = -EMLINK;
759         ENTRY;
760
761         if (dir->i_nlink >= EXT2_LINK_MAX)
762                 RETURN(err);
763
764         err = mdc_create(&sbi->ll_mdc_conn, dir, name, len,
765                          tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
766                          current->fsuid, current->fsgid, time, 0, &request);
767         ptlrpc_req_finished(request);
768         RETURN(err);
769 }
770
771 static int ll_symlink(struct inode *dir, struct dentry *dentry,
772                       const char *symname)
773 {
774         struct lookup_intent *it;
775         unsigned l = strlen(symname) + 1;
776         struct inode *inode;
777         struct ll_inode_info *lli;
778         int err = 0;
779         ENTRY;
780
781         LL_GET_INTENT(dentry, it);
782
783         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
784                                symname, l, S_IFLNK | S_IRWXUGO, 0, it);
785         if (IS_ERR(inode))
786                 RETURN(PTR_ERR(inode));
787
788         lli = ll_i2info(inode);
789
790         OBD_ALLOC(lli->lli_symlink_name, l);
791         /* this _could_ be a non-fatal error, since the symlink is already
792          * stored on the MDS by this point, and we can re-get it in readlink.
793          */
794         if (!lli->lli_symlink_name)
795                 RETURN(-ENOMEM);
796
797         memcpy(lli->lli_symlink_name, symname, l);
798         inode->i_size = l - 1;
799
800         /* no directory data updates when intents rule */
801         if (it && it->it_disposition)
802                 d_instantiate(dentry, inode);
803         else
804                 err = ext2_add_nondir(dentry, inode);
805
806         RETURN(err);
807 }
808
809 static int ll_link2(struct inode *src, struct inode *dir,
810                     const char *name, int len)
811 {
812         struct ptlrpc_request *request = NULL;
813         int err;
814         struct ll_sb_info *sbi = ll_i2sbi(dir);
815
816         ENTRY;
817
818         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name, len, &request);
819         ptlrpc_req_finished(request);
820
821         RETURN(err);
822 }
823
824 static int ll_link(struct dentry *old_dentry, struct inode * dir,
825                    struct dentry *dentry)
826 {
827         struct lookup_intent *it;
828         struct inode *inode = old_dentry->d_inode;
829         int rc;
830
831         LL_GET_INTENT(dentry, it);
832
833         if (it && it->it_disposition) {
834                 if (it->it_status)
835                         RETURN(it->it_status);
836                 inode->i_ctime = CURRENT_TIME;
837                 ext2_inc_count(inode);
838                 atomic_inc(&inode->i_count);
839                 d_instantiate(dentry, inode);
840                 ll_invalidate_inode_pages(dir);
841                 RETURN(0);
842         }
843
844         if (S_ISDIR(inode->i_mode))
845                 return -EPERM;
846
847         if (inode->i_nlink >= EXT2_LINK_MAX)
848                 return -EMLINK;
849
850         rc = ll_link2(old_dentry->d_inode, dir,
851                       dentry->d_name.name, dentry->d_name.len);
852         if (rc)
853                 RETURN(rc);
854
855         inode->i_ctime = CURRENT_TIME;
856         ext2_inc_count(inode);
857         atomic_inc(&inode->i_count);
858
859         return ext2_add_nondir(dentry, inode);
860 }
861
862 static int ll_mkdir2(struct inode *dir, const char *name, int len, int mode)
863 {
864         struct ptlrpc_request *request = NULL;
865         time_t time = CURRENT_TIME;
866         struct ll_sb_info *sbi = ll_i2sbi(dir);
867         int err = -EMLINK;
868         ENTRY;
869
870         if (dir->i_nlink >= EXT2_LINK_MAX)
871                 RETURN(err);
872
873         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
874         err = mdc_create(&sbi->ll_mdc_conn, dir, name, len, NULL, 0,
875                          mode, current->fsuid, current->fsgid,
876                          time, 0, &request);
877         ptlrpc_req_finished(request);
878         RETURN(err);
879 }
880
881
882 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
883 {
884         struct lookup_intent *it;
885         struct inode * inode;
886         int err = -EMLINK;
887         ENTRY;
888
889         LL_GET_INTENT(dentry, it);
890
891         if (dir->i_nlink >= EXT2_LINK_MAX)
892                 goto out;
893
894         ext2_inc_count(dir);
895         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
896                                NULL, 0, S_IFDIR | mode, 0, it);
897         err = PTR_ERR(inode);
898         if (IS_ERR(inode))
899                 goto out_dir;
900
901         err = ext2_make_empty(inode, dir);
902         if (err)
903                 goto out_fail;
904
905         /* no directory data updates when intents rule */
906         if (!it || !it->it_disposition) {
907                 /* XXX FIXME This code needs re-checked for non-intents */
908                 ext2_inc_count(inode);
909                 err = ll_add_link(dentry, inode);
910                 if (err)
911                         goto out_fail;
912         }
913
914         d_instantiate(dentry, inode);
915 out:
916         EXIT;
917         return err;
918
919 out_fail:
920         ext2_dec_count(inode);
921         ext2_dec_count(inode);
922         iput(inode);
923         EXIT;
924 out_dir:
925         ext2_dec_count(dir);
926         EXIT;
927         goto out;
928 }
929
930 static int ll_rmdir2(struct inode *dir, const char *name, int len)
931 {
932         int rc;
933         ENTRY;
934
935         rc = ll_mdc_unlink(dir, NULL, S_IFDIR, name, len);
936         RETURN(rc);
937 }
938
939 static int ll_unlink2(struct inode *dir, const char *name, int len)
940 {
941         int rc;
942         ENTRY;
943
944         rc = ll_mdc_unlink(dir, NULL, S_IFREG, name, len);
945         RETURN(rc);
946 }
947
948 static int ll_common_unlink(struct inode *dir, struct dentry *dentry,
949                             struct lookup_intent *it, __u32 mode)
950 {
951         struct inode *inode = dentry->d_inode;
952         struct ext2_dir_entry_2 * de;
953         struct page * page;
954         int rc = 0;
955         ENTRY;
956
957         if (it && it->it_disposition) {
958                 rc = it->it_status;
959                 ll_invalidate_inode_pages(dir);
960                 if (rc)
961                         GOTO(out, rc);
962                 GOTO(out_dec, 0);
963         }
964
965         de = ext2_find_entry(dir, dentry, &page);
966         if (!de)
967                 GOTO(out, rc = -ENOENT);
968         rc = ll_mdc_unlink(dir, dentry->d_inode, mode,
969                            dentry->d_name.name, dentry->d_name.len);
970         if (rc)
971                 GOTO(out, rc);
972
973         rc = ext2_delete_entry(de, page);
974         if (rc)
975                 GOTO(out, rc);
976
977         /* AED: not sure if needed - directory lock revocation should do it
978          * in the case where the client has cached it for non-intent ops.
979          */
980         ll_invalidate_inode_pages(dir);
981
982         inode->i_ctime = dir->i_ctime;
983         EXIT;
984 out_dec:
985         ext2_dec_count(inode);
986 out:
987         return rc;
988 }
989
990 static int ll_unlink(struct inode *dir, struct dentry *dentry)
991 {
992         struct lookup_intent * it;
993         ENTRY;
994
995         LL_GET_INTENT(dentry, it);
996
997         RETURN(ll_common_unlink(dir, dentry, it, S_IFREG));
998 }
999
1000 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1001 {
1002         struct inode * inode = dentry->d_inode;
1003         struct lookup_intent *it;
1004         int rc;
1005         ENTRY;
1006
1007         LL_GET_INTENT(dentry, it);
1008
1009         if ((!it || !it->it_disposition) && !ext2_empty_dir(inode))
1010                 RETURN(-ENOTEMPTY);
1011
1012         rc = ll_common_unlink(dir, dentry, it, S_IFDIR);
1013         if (!rc) {
1014                 inode->i_size = 0;
1015                 ext2_dec_count(inode);
1016                 ext2_dec_count(dir);
1017         }
1018
1019         RETURN(rc);
1020 }
1021
1022 static int ll_rename2(struct inode *src, struct inode *tgt,
1023                       const char *oldname, int oldlen,
1024                       const char *newname, int newlen)
1025 {
1026         struct ptlrpc_request *request = NULL;
1027         struct ll_sb_info *sbi = ll_i2sbi(src);
1028         int err;
1029         ENTRY;
1030
1031         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
1032                          oldname, oldlen, newname, newlen, &request);
1033         ptlrpc_req_finished(request);
1034
1035         RETURN(err);
1036 }
1037
1038
1039
1040 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
1041                      struct inode * new_dir, struct dentry * new_dentry)
1042 {
1043         struct lookup_intent *it;
1044         struct inode * old_inode = old_dentry->d_inode;
1045         struct inode * tgt_inode = new_dentry->d_inode;
1046         struct page * dir_page = NULL;
1047         struct ext2_dir_entry_2 * dir_de = NULL;
1048         struct ext2_dir_entry_2 * old_de;
1049         struct page * old_page;
1050         int err;
1051
1052         LL_GET_INTENT(new_dentry, it);
1053
1054         if (it && it->it_disposition) {
1055                 if (tgt_inode) {
1056                         tgt_inode->i_ctime = CURRENT_TIME;
1057                         tgt_inode->i_nlink--;
1058                 }
1059                 ll_invalidate_inode_pages(old_dir);
1060                 ll_invalidate_inode_pages(new_dir);
1061                 GOTO(out, err = it->it_status);
1062         }
1063
1064         err = ll_rename2(old_dir, new_dir,
1065                          old_dentry->d_name.name, old_dentry->d_name.len,
1066                          new_dentry->d_name.name, new_dentry->d_name.len);
1067         if (err)
1068                 goto out;
1069
1070         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
1071         if (!old_de)
1072                 goto out;
1073
1074         if (S_ISDIR(old_inode->i_mode)) {
1075                 err = -EIO;
1076                 dir_de = ext2_dotdot(old_inode, &dir_page);
1077                 if (!dir_de)
1078                         goto out_old;
1079         }
1080
1081         if (tgt_inode) {
1082                 struct page *new_page;
1083                 struct ext2_dir_entry_2 *new_de;
1084
1085                 err = -ENOTEMPTY;
1086                 if (dir_de && !ext2_empty_dir (tgt_inode))
1087                         goto out_dir;
1088
1089                 err = -ENOENT;
1090                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
1091                 if (!new_de)
1092                         goto out_dir;
1093                 ext2_inc_count(old_inode);
1094                 ext2_set_link(new_dir, new_de, new_page, old_inode);
1095                 tgt_inode->i_ctime = CURRENT_TIME;
1096                 if (dir_de)
1097                         tgt_inode->i_nlink--;
1098                 ext2_dec_count(tgt_inode);
1099         } else {
1100                 if (dir_de) {
1101                         err = -EMLINK;
1102                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
1103                                 goto out_dir;
1104                 }
1105                 ext2_inc_count(old_inode);
1106                 err = ll_add_link(new_dentry, old_inode);
1107                 if (err) {
1108                         ext2_dec_count(old_inode);
1109                         goto out_dir;
1110                 }
1111                 if (dir_de)
1112                         ext2_inc_count(new_dir);
1113         }
1114
1115         ext2_delete_entry (old_de, old_page);
1116         ext2_dec_count(old_inode);
1117
1118         if (dir_de) {
1119                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
1120                 ext2_dec_count(old_dir);
1121         }
1122         return 0;
1123
1124 out_dir:
1125         if (dir_de) {
1126                 kunmap(dir_page);
1127                 page_cache_release(dir_page);
1128         }
1129 out_old:
1130         kunmap(old_page);
1131         page_cache_release(old_page);
1132 out:
1133         return err;
1134 }
1135
1136 extern int ll_inode_revalidate(struct dentry *dentry);
1137 struct inode_operations ll_dir_inode_operations = {
1138         create:          ll_create,
1139         lookup2:         ll_lookup2,
1140         link:            ll_link,
1141         link2:           ll_link2,
1142         unlink:          ll_unlink,
1143         unlink2:         ll_unlink2,
1144         symlink:         ll_symlink,
1145         symlink2:        ll_symlink2,
1146         mkdir:           ll_mkdir,
1147         mkdir2:          ll_mkdir2,
1148         rmdir:           ll_rmdir,
1149         rmdir2:          ll_rmdir2,
1150         mknod:           ll_mknod,
1151         mknod2:          ll_mknod2,
1152         rename:          ll_rename,
1153         rename2:         ll_rename2,
1154         setattr:         ll_setattr,
1155         revalidate:      ll_inode_revalidate,
1156 };