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