Whamcloud - gitweb
- landed b_hd_cray_merge3
[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 #include <linux/lustre_version.h>
45 #include "llite_internal.h"
46
47 /* methods */
48
49 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
50 static int ll_test_inode(struct inode *inode, unsigned long ino, void *opaque)
51 #else
52 static int ll_test_inode(struct inode *inode, void *opaque)
53 #endif
54 {
55         static int last_ino, last_gen, last_count;
56         struct lustre_md *md = opaque;
57
58         if (!(md->body->valid & (OBD_MD_FLGENER | OBD_MD_FLID))) {
59                 CERROR("MDS body missing inum or generation\n");
60                 return 0;
61         }
62
63         if (last_ino == id_ino(&md->body->id1) &&
64             last_gen == id_gen(&md->body->id1) &&
65             last_count < 500) {
66                 last_count++;
67         } else {
68                 if (last_count > 1)
69                         CDEBUG(D_VFSTRACE, "compared %u/%u %u times\n",
70                                last_ino, last_gen, last_count);
71                 last_count = 0;
72                 last_ino = id_ino(&md->body->id1);
73                 last_gen = id_gen(&md->body->id1);
74                 CDEBUG(D_VFSTRACE,
75                        "comparing inode %p ino "DLID4" to body "DLID4"\n",
76                        inode, OLID4(&ll_i2info(inode)->lli_id),
77                        OLID4(&md->body->id1));
78         }
79
80 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
81         if (inode->i_ino != id_ino(&md->body->id1))
82                 return 0;
83 #endif
84         if (inode->i_generation != id_gen(&md->body->id1))
85                 return 0;
86
87         if (id_group(&ll_i2info(inode)->lli_id) != id_group(&md->body->id1))
88                 return 0;
89         
90         /* apply the attributes in 'opaque' to this inode. */
91         ll_update_inode(inode, md);
92         return 1;
93 }
94
95 extern struct dentry_operations ll_d_ops;
96
97 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
98 {
99         ENTRY;
100
101         ldlm_lock_decref(lockh, mode);
102
103         RETURN(0);
104 }
105
106 /*
107  * get an inode by inode number (already instantiated by the intent lookup).
108  * Returns inode or NULL.
109  */
110 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
111 int ll_set_inode(struct inode *inode, void *opaque)
112 {
113         ll_read_inode2(inode, opaque);
114         return 0;
115 }
116
117 struct inode *ll_iget(struct super_block *sb, ino_t hash,
118                       struct lustre_md *md)
119 {
120         struct inode *inode;
121
122         LASSERT(hash != 0);
123         inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
124
125         if (inode) {
126                 if (inode->i_state & I_NEW)
127                         unlock_new_inode(inode);
128                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
129                        inode->i_generation, inode);
130         }
131
132         return inode;
133 }
134 #else
135 struct inode *ll_iget(struct super_block *sb, ino_t hash,
136                       struct lustre_md *md)
137 {
138         struct inode *inode;
139         LASSERT(hash != 0);
140         inode = iget4(sb, hash, ll_test_inode, md);
141         if (inode)
142                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
143                        inode->i_generation, inode);
144         return inode;
145 }
146 #endif
147
148 int ll_mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
149                         void *data, int flag)
150 {
151         int rc;
152         struct lustre_handle lockh;
153         ENTRY;
154
155         switch (flag) {
156         case LDLM_CB_BLOCKING:
157                 ldlm_lock2handle(lock, &lockh);
158                 rc = ldlm_cli_cancel(&lockh);
159                 if (rc < 0) {
160                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
161                         RETURN(rc);
162                 }
163                 break;
164         case LDLM_CB_CANCELING: {
165                 struct inode *inode = ll_inode_from_lock(lock);
166                 struct ll_inode_info *li = ll_i2info(inode);
167                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
168
169                 /* For lookup locks: Invalidate all dentries associated with
170                    this inode, for UPDATE locks - invalidate directory pages */
171                 if (inode == NULL)
172                         break;
173
174                 if (bits & MDS_INODELOCK_UPDATE)
175                         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
176                                   &(ll_i2info(inode)->lli_flags));
177
178
179                 if (lock->l_resource->lr_name.name[0] != id_fid(&li->lli_id) ||
180                     lock->l_resource->lr_name.name[1] != id_group(&li->lli_id)) {
181                         LDLM_ERROR(lock, "data mismatch with object %lu/%lu",
182                                    (unsigned long)id_fid(&li->lli_id),
183                                    (unsigned long)id_group(&li->lli_id));
184                 }
185
186                 if (bits & MDS_INODELOCK_OPEN) {
187                         int flags = 0;
188                         switch (lock->l_req_mode) {
189                         case LCK_CW:
190                                 flags = FMODE_WRITE;
191                                 break;
192                         case LCK_PR:
193                                 flags = FMODE_EXEC;
194                                 break;
195                         case LCK_CR:
196                                 flags = FMODE_READ;
197                                 break;
198                         default:
199                                 CERROR("Unexpected lock mode for OPEN lock "
200                                        "%d, inode %ld\n", lock->l_req_mode,
201                                        inode->i_ino);
202                         }
203                         ll_md_real_close(ll_i2mdexp(inode), inode, flags);
204                 }
205
206                 if (bits & MDS_INODELOCK_UPDATE)
207                         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
208                                   &(ll_i2info(inode)->lli_flags));
209
210
211                 /* If lookup lock is cancelled, we just drop the dentry and
212                    this will cause us to reget data from MDS when we'd want to
213                    access this dentry/inode again. If this is lock on
214                    other parts of inode that is cancelled, we do not need to do
215                    much (but need to discard data from readdir, if any), since
216                    abscence of lock will cause ll_revalidate_it (called from
217                    stat() and similar functions) to renew the data anyway */
218                 if (S_ISDIR(inode->i_mode) &&
219                     (bits & MDS_INODELOCK_UPDATE)) {
220                         CDEBUG(D_INODE, "invalidating inode %lu/%u(%p)\n",
221                                inode->i_ino, inode->i_generation, inode);
222                         truncate_inode_pages(inode->i_mapping, 0);
223                 }
224
225                 if (inode->i_sb->s_root &&
226                     inode != inode->i_sb->s_root->d_inode &&
227                     (bits & MDS_INODELOCK_LOOKUP))
228                         ll_unhash_aliases(inode);
229                 iput(inode);
230                 break;
231         }
232         default:
233                 LBUG();
234         }
235
236         RETURN(0);
237 }
238
239 int ll_mdc_cancel_unused(struct lustre_handle *conn, struct inode *inode,
240                          int flags, void *opaque)
241 {
242         struct ll_inode_info *li = ll_i2info(inode);
243         struct ldlm_res_id res_id =
244                 { .name = {id_fid(&li->lli_id), id_group(&li->lli_id)} };
245         struct obd_device *obddev = class_conn2obd(conn);
246         ENTRY;
247         
248         RETURN(ldlm_cli_cancel_unused(obddev->obd_namespace, &res_id, flags,
249                                       opaque));
250 }
251
252 /* Search "inode"'s alias list for a dentry that has the same name and parent as
253  * de.  If found, return it.  If not found, return de. */
254 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
255 {
256         struct list_head *tmp;
257
258         spin_lock(&dcache_lock);
259         list_for_each(tmp, &inode->i_dentry) {
260                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
261
262                 /* We are called here with 'de' already on the aliases list. */
263                 if (dentry == de) {
264                         CERROR("whoops\n");
265                         continue;
266                 }
267
268                 if (dentry->d_parent != de->d_parent)
269                         continue;
270
271                 if (dentry->d_name.len != de->d_name.len)
272                         continue;
273
274                 if (memcmp(dentry->d_name.name, de->d_name.name,
275                            de->d_name.len) != 0)
276                         continue;
277
278                 if (!list_empty(&dentry->d_lru))
279                         list_del_init(&dentry->d_lru);
280
281                 hlist_del_init(&dentry->d_hash);
282                 __d_rehash(dentry); /* avoid taking dcache_lock inside */
283                 spin_unlock(&dcache_lock);
284                 atomic_inc(&dentry->d_count);
285                 iput(inode);
286                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
287                 CDEBUG(D_DENTRY, "alias dentry %*s (%p) parent %p inode %p "
288                        "refc %d\n", de->d_name.len, de->d_name.name, de,
289                        de->d_parent, de->d_inode, atomic_read(&de->d_count));
290                 return dentry;
291         }
292
293         spin_unlock(&dcache_lock);
294
295         return de;
296 }
297
298 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
299                             struct lookup_intent *it, void *data)
300 {
301         struct it_cb_data *icbd = data;
302         struct dentry **de = icbd->icbd_childp;
303         struct inode *parent = icbd->icbd_parent;
304         struct ll_sb_info *sbi = ll_i2sbi(parent);
305         struct dentry *dentry = *de, *saved = *de;
306         struct inode *inode = NULL;
307         int rc;
308
309         /* NB 1 request reference will be taken away by ll_intent_lock()
310          * when I return */
311         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
312                 ENTRY;
313
314                 rc = ll_prep_inode(sbi->ll_dt_exp, sbi->ll_md_exp,
315                                    &inode, request, offset, dentry->d_sb);
316                 if (rc)
317                         RETURN(rc);
318
319                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
320                        inode, inode->i_ino, inode->i_generation);
321                 
322                 mdc_set_lock_data(NULL, &LUSTRE_IT(it)->it_lock_handle, inode);
323                 
324                 /* If this is a stat, get the authoritative file size */
325                 if (it->it_op == IT_GETATTR && S_ISREG(inode->i_mode) &&
326                     ll_i2info(inode)->lli_smd != NULL) {
327                         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
328                         ldlm_error_t rc;
329
330                         LASSERT(lsm->lsm_object_id != 0);
331
332                         /* bug 2334: drop MDS lock before acquiring OST lock */
333                         ll_intent_drop_lock(it);
334
335                         rc = ll_glimpse_size(inode);
336                         if (rc) {
337                                 iput(inode);
338                                 RETURN(rc);
339                         }
340                 }
341
342                 dentry = *de = ll_find_alias(inode, dentry);
343         } else {
344                 ENTRY;
345         }
346
347         dentry->d_op = &ll_d_ops;
348         ll_set_dd(dentry);
349
350         if (dentry == saved)
351                 d_add(dentry, inode);
352
353         RETURN(0);
354 }
355
356 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
357                                    struct nameidata *nd, int flags)
358 {
359         struct dentry *save = dentry, *retval;
360         struct lookup_intent *it = flags ? &nd->intent.open : NULL;
361         struct lustre_id pid;
362         struct it_cb_data icbd;
363         struct ptlrpc_request *req = NULL;
364         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
365         int rc, orig_it;
366         ENTRY;
367
368         if (dentry->d_name.len > EXT3_NAME_LEN)
369                 RETURN(ERR_PTR(-ENAMETOOLONG));
370
371         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
372                dentry->d_name.len, dentry->d_name.name, parent->i_ino,
373                parent->i_generation, parent, LL_IT2STR(it));
374
375         if (d_mountpoint(dentry))
376                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
377
378         if (nd != NULL)
379                 nd->mnt->mnt_last_used = jiffies;
380
381         orig_it = it ? it->it_op : IT_OPEN;
382         ll_frob_intent(&it, &lookup_it);
383
384         icbd.icbd_childp = &dentry;
385         icbd.icbd_parent = parent;
386         ll_inode2id(&pid, parent);
387
388         rc = md_intent_lock(ll_i2mdexp(parent), &pid,
389                             dentry->d_name.name, dentry->d_name.len, NULL, 0,
390                             NULL, it, flags, &req, ll_mdc_blocking_ast);
391         if (rc < 0)
392                 GOTO(out, retval = ERR_PTR(rc));
393
394         rc = lookup_it_finish(req, 1, it, &icbd);
395         if (rc != 0) {
396                 ll_intent_release(it);
397                 GOTO(out, retval = ERR_PTR(rc));
398         }
399
400         ll_lookup_finish_locks(it, dentry);
401
402         if (nd &&
403             dentry->d_inode != NULL && dentry->d_inode->i_mode & S_ISUID &&
404             S_ISDIR(dentry->d_inode->i_mode) &&
405             ((flags & LOOKUP_CONTINUE) || (orig_it & (IT_CHDIR | IT_OPEN))))
406         {
407                 spin_lock(&dentry->d_lock);
408                 dentry->d_flags |= DCACHE_GNS_PENDING;
409                 spin_unlock(&dentry->d_lock);
410         }
411
412         if (dentry == save)
413                 GOTO(out, retval = NULL);
414         else
415                 GOTO(out, retval = dentry);
416  out:
417         if (req)
418                 ptlrpc_req_finished(req);
419         if (it == &lookup_it)
420                 ll_intent_release(it);
421         if (dentry->d_inode)
422                 CDEBUG(D_INODE, "lookup 0x%p in %lu/%lu: %*s -> %lu/%lu\n",
423                        dentry,
424                        (unsigned long) parent->i_ino,
425                        (unsigned long) parent->i_generation,
426                        dentry->d_name.len, dentry->d_name.name,
427                        (unsigned long) dentry->d_inode->i_ino,
428                        (unsigned long) dentry->d_inode->i_generation);
429         else
430                 CDEBUG(D_INODE, "lookup 0x%p in %lu/%lu: %*s -> ??\n",
431                        dentry,
432                        (unsigned long) parent->i_ino,
433                        (unsigned long) parent->i_generation,
434                        dentry->d_name.len, dentry->d_name.name);
435         return retval;
436 }
437
438 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
439 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
440                                    struct nameidata *nd)
441 {
442         struct dentry *de;
443         ENTRY;
444
445         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
446                 de = ll_lookup_it(parent, dentry, nd, nd->flags);
447         else
448                 de = ll_lookup_it(parent, dentry, nd, 0);
449
450         RETURN(de);
451 }
452 #endif
453
454 /* We depend on "mode" being set with the proper file type/umask by now */
455 static struct inode *ll_create_node(struct inode *dir, const char *name,
456                                     int namelen, const void *data, int datalen,
457                                     int mode, __u64 extra,
458                                     struct lookup_intent *it)
459 {
460         struct inode *inode = NULL;
461         struct ptlrpc_request *request = NULL;
462         struct ll_sb_info *sbi = ll_i2sbi(dir);
463         int rc;
464         ENTRY;
465
466
467         LASSERT(it && LUSTRE_IT(it)->it_disposition);
468   
469         request = LUSTRE_IT(it)->it_data;
470         rc = ll_prep_inode(sbi->ll_dt_exp, sbi->ll_md_exp,
471                            &inode, request, 1, dir->i_sb);
472         if (rc)
473                 GOTO(out, inode = ERR_PTR(rc));
474
475         LASSERT(list_empty(&inode->i_dentry));
476
477         /* We asked for a lock on the directory, but were granted a
478          * lock on the inode.  Since we finally have an inode pointer,
479          * stuff it in the lock. */
480         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
481                inode, inode->i_ino, inode->i_generation);
482         mdc_set_lock_data(NULL, &LUSTRE_IT(it)->it_lock_handle, inode);
483         EXIT;
484  out:
485         ptlrpc_req_finished(request);
486         return inode;
487 }
488
489 /*
490  * By the time this is called, we already have created the directory cache
491  * entry for the new file, but it is so far negative - it has no inode.
492  *
493  * We defer creating the OBD object(s) until open, to keep the intent and
494  * non-intent code paths similar, and also because we do not have the MDS
495  * inode number before calling ll_create_node() (which is needed for LOV),
496  * so we would need to do yet another RPC to the MDS to store the LOV EA
497  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
498  * lmm_size in datalen (the MDS still has code which will handle that).
499  *
500  * If the create succeeds, we fill in the inode information
501  * with d_instantiate().
502  */
503 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
504                         struct lookup_intent *it)
505 {
506         struct inode *inode;
507         struct ptlrpc_request *request = LUSTRE_IT(it)->it_data;
508         struct obd_export *md_exp = ll_i2mdexp(dir); 
509         int rc = 0;
510         ENTRY;
511
512         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
513                dentry->d_name.len, dentry->d_name.name, dir->i_ino,
514                dir->i_generation, dir, LL_IT2STR(it));
515
516         rc = it_open_error(DISP_OPEN_CREATE, it);
517         if (rc)
518                 RETURN(rc);
519
520         mdc_store_inode_generation(md_exp, request, MDS_REQ_INTENT_REC_OFF, 1);
521         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
522                                NULL, 0, mode, 0, it);
523         if (IS_ERR(inode))
524                 RETURN(PTR_ERR(inode));
525
526         d_instantiate(dentry, inode);
527         RETURN(0);
528 }
529
530 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
531 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
532 {
533         return ll_create_it(dir, dentry, mode, &nd->intent.open);
534 }
535 #endif
536
537 static void ll_update_times(struct ptlrpc_request *request, int offset,
538                             struct inode *inode)
539 {
540         struct mds_body *body = lustre_msg_buf(request->rq_repmsg, offset,
541                                                sizeof(*body));
542         LASSERT(body);
543
544         if (body->valid & OBD_MD_FLMTIME &&
545             body->mtime > LTIME_S(inode->i_mtime)) {
546                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
547                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
548                 LTIME_S(inode->i_mtime) = body->mtime;
549         }
550         if (body->valid & OBD_MD_FLCTIME &&
551             body->ctime > LTIME_S(inode->i_ctime))
552                 LTIME_S(inode->i_ctime) = body->ctime;
553 }
554
555 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
556 {
557         struct ptlrpc_request *request = NULL;
558         struct inode *dir = nd->dentry->d_inode;
559         struct ll_sb_info *sbi = ll_i2sbi(dir);
560         struct mdc_op_data *op_data;
561         int err = -EMLINK;
562         ENTRY;
563
564         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
565                nd->last.len, nd->last.name, dir->i_ino, dir->i_generation, dir);
566
567         mode &= ~current->fs->umask;
568
569         switch (mode & S_IFMT) {
570         case 0:
571         case S_IFREG:
572                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
573         case S_IFCHR:
574         case S_IFBLK:
575         case S_IFIFO:
576         case S_IFSOCK:
577                 OBD_ALLOC(op_data, sizeof(*op_data));
578                 if (op_data == NULL)
579                         RETURN(-ENOMEM);
580                 ll_prepare_mdc_data(op_data, dir, NULL, nd->last.name, 
581                                     nd->last.len, 0);
582                 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
583                                 current->fsuid, current->fsgid, rdev,
584                                 &request);
585                 OBD_FREE(op_data, sizeof(*op_data));
586                 if (err == 0)
587                         ll_update_times(request, 0, dir);
588                 ptlrpc_req_finished(request);
589                 break;
590         case S_IFDIR:
591                 err = -EPERM;
592                 break;
593         default:
594                 err = -EINVAL;
595         }
596         RETURN(err);
597 }
598
599 static int ll_mknod(struct inode *dir, struct dentry *dchild,
600                     int mode, ll_dev_t rdev)
601 {
602         struct ptlrpc_request *request = NULL;
603         struct inode *inode = NULL;
604         struct ll_sb_info *sbi = ll_i2sbi(dir);
605         struct mdc_op_data *op_data;
606         int err = -EMLINK;
607         ENTRY;
608
609         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
610                dchild->d_name.len, dchild->d_name.name,
611                dir->i_ino, dir->i_generation, dir);
612
613         mode &= ~current->fs->umask;
614
615         switch (mode & S_IFMT) {
616         case 0:
617         case S_IFREG:
618                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
619         case S_IFCHR:
620         case S_IFBLK:
621         case S_IFIFO:
622         case S_IFSOCK:
623                 OBD_ALLOC(op_data, sizeof(*op_data));
624                 if (op_data == NULL)
625                         RETURN(-ENOMEM);
626                 ll_prepare_mdc_data(op_data, dir, NULL, dchild->d_name.name, 
627                                     dchild->d_name.len, 0);
628                 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
629                                 current->fsuid, current->fsgid, rdev,
630                                 &request);
631                 OBD_FREE(op_data, sizeof(*op_data));
632                 if (err)
633                         GOTO(out_err, err);
634
635                 ll_update_times(request, 0, dir);
636                 err = ll_prep_inode(sbi->ll_dt_exp, sbi->ll_md_exp,
637                                     &inode, request, 0, dchild->d_sb);
638                 if (err)
639                         GOTO(out_err, err);
640                 break;
641         case S_IFDIR:
642                 RETURN(-EPERM);
643                 break;
644         default:
645                 RETURN(-EINVAL);
646         }
647
648         d_instantiate(dchild, inode);
649         EXIT;
650  out_err:
651         ptlrpc_req_finished(request);
652         return err;
653 }
654
655 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
656 {
657         struct inode *dir = nd->dentry->d_inode;
658         struct ptlrpc_request *request = NULL;
659         struct ll_sb_info *sbi = ll_i2sbi(dir);
660         const char *name = nd->last.name;
661         struct mdc_op_data *op_data;
662         int len = nd->last.len;
663         int err = -EMLINK;
664         ENTRY;
665
666         CDEBUG(D_VFSTRACE, "VFS Op:name=%*s,dir=%lu/%u(%p),target=%s\n",
667                nd->last.len, nd->last.name, dir->i_ino, dir->i_generation,
668                dir, tgt);
669
670         if (dir->i_nlink >= EXT3_LINK_MAX)
671                 RETURN(err);
672
673         OBD_ALLOC(op_data, sizeof(*op_data));
674         if (op_data == NULL)
675                 RETURN(-ENOMEM);
676         ll_prepare_mdc_data(op_data, dir, NULL, name, len, 0);
677         LASSERT(tgt);
678         err = md_create(sbi->ll_md_exp, op_data,
679                         tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
680                         current->fsuid, current->fsgid, 0, &request);
681         OBD_FREE(op_data, sizeof(*op_data));
682         if (err == 0)
683                 ll_update_times(request, 0, dir);
684         
685         ptlrpc_req_finished(request);
686         RETURN(err);
687 }
688
689 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
690 {
691         struct inode *src = srcnd->dentry->d_inode;
692         struct inode *dir = tgtnd->dentry->d_inode;
693         struct ptlrpc_request *request = NULL;
694         struct mdc_op_data *op_data;
695         int err;
696         struct ll_sb_info *sbi = ll_i2sbi(dir);
697         ENTRY;
698
699         CDEBUG(D_VFSTRACE,
700                "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
701                src->i_ino, src->i_generation, src, dir->i_ino,
702                dir->i_generation, dir, tgtnd->last.len, tgtnd->last.name);
703
704         OBD_ALLOC(op_data, sizeof(*op_data));
705         if (op_data == NULL)
706                 RETURN(-ENOMEM);
707         ll_prepare_mdc_data(op_data, src, dir, tgtnd->last.name, 
708                             tgtnd->last.len, 0);
709         err = md_link(sbi->ll_md_exp, op_data, &request);
710         OBD_FREE(op_data, sizeof(*op_data));
711         if (err == 0)
712                 ll_update_times(request, 0, dir);
713         ptlrpc_req_finished(request);
714         RETURN(err);
715 }
716
717
718 static int ll_mkdir_raw(struct nameidata *nd, int mode)
719 {
720         struct inode *dir = nd->dentry->d_inode;
721         struct ptlrpc_request *request = NULL;
722         struct ll_sb_info *sbi = ll_i2sbi(dir);
723         struct mdc_op_data *op_data;
724         int err = -EMLINK;
725         ENTRY;
726         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
727                nd->last.len, nd->last.name, dir->i_ino, dir->i_generation, dir);
728
729         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
730         OBD_ALLOC(op_data, sizeof(*op_data));
731         if (op_data == NULL)
732                 RETURN(-ENOMEM);
733         ll_prepare_mdc_data(op_data, dir, NULL, nd->last.name, 
734                             nd->last.len, 0);
735         err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
736                         current->fsuid, current->fsgid, 0, &request);
737         OBD_FREE(op_data, sizeof(*op_data));
738         if (err == 0)
739                 ll_update_times(request, 0, dir);
740         ptlrpc_req_finished(request);
741         RETURN(err);
742 }
743
744 static int ll_rmdir_raw(struct nameidata *nd)
745 {
746         struct inode *dir = nd->dentry->d_inode;
747         struct ptlrpc_request *request = NULL;
748         struct mdc_op_data *op_data;
749         int rc;
750
751         ENTRY;
752         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
753                nd->last.len, nd->last.name, dir->i_ino, dir->i_generation, dir);
754
755         OBD_ALLOC(op_data, sizeof(*op_data));
756         if (op_data == NULL)
757                 RETURN(-ENOMEM);
758         ll_prepare_mdc_data(op_data, dir, NULL, nd->last.name, 
759                             nd->last.len, S_IFDIR);
760         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
761         OBD_FREE(op_data, sizeof(*op_data));
762         if (rc == 0)
763                 ll_update_times(request, 0, dir);
764         ptlrpc_req_finished(request);
765         RETURN(rc);
766 }
767
768 int ll_objects_destroy(struct ptlrpc_request *request,
769                        struct inode *dir, int offset)
770 {
771         struct mds_body *body;
772         struct lov_mds_md *eadata;
773         struct lov_stripe_md *lsm = NULL;
774         struct obd_trans_info oti = { 0 };
775         struct obdo *oa;
776         int rc;
777         ENTRY;
778
779         /* req is swabbed so this is safe */
780         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
781
782         if (!(body->valid & OBD_MD_FLEASIZE))
783                 RETURN(0);
784
785         if (body->eadatasize == 0) {
786                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
787                 GOTO(out, rc = -EPROTO);
788         }
789
790         /*
791          * the MDS sent back the EA because we unlinked the last reference to
792          * this file. Use this EA to unlink the objects on the OST. It's opaque
793          * so we don't swab here; we leave it to obd_unpackmd() to check it is
794          * complete and sensible.
795          */
796         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
797         LASSERT(eadata != NULL);
798         if (eadata == NULL) {
799                 CERROR("Can't unpack MDS EA data\n");
800                 GOTO(out, rc = -EPROTO);
801         }
802
803         rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
804         if (rc < 0) {
805                 CERROR("obd_unpackmd: %d\n", rc);
806                 GOTO(out, rc);
807         }
808         LASSERT(rc >= sizeof(*lsm));
809
810         oa = obdo_alloc();
811         if (oa == NULL)
812                 GOTO(out_free_memmd, rc = -ENOMEM);
813
814         oa->o_id = lsm->lsm_object_id;
815         oa->o_gr = lsm->lsm_object_gr;
816         oa->o_mode = body->mode & S_IFMT;
817         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
818
819         if (body->valid & OBD_MD_FLCOOKIE) {
820                 int length = sizeof(struct llog_cookie) *
821                                 lsm->lsm_stripe_count;
822                 oa->o_valid |= OBD_MD_FLCOOKIE;
823                 oti.oti_logcookies =
824                         lustre_msg_buf(request->rq_repmsg, 2, length);
825                 if (oti.oti_logcookies == NULL) {
826                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
827                         body->valid &= ~OBD_MD_FLCOOKIE;
828                 } else {
829                         /* copy llog cookies to request to replay unlink
830                          * so that the same llog file and records as those created
831                          * during fail can be re-created while doing replay 
832                          */
833                         if (offset >= 0)
834                                 memcpy(lustre_msg_buf(request->rq_reqmsg, offset, 0),
835                                        oti.oti_logcookies, length);
836                 }
837         }
838
839         rc = obd_destroy(ll_i2dtexp(dir), oa, lsm, &oti);
840         obdo_free(oa);
841         if (rc)
842                 CERROR("obd destroy objid "LPX64" error %d\n",
843                        lsm->lsm_object_id, rc);
844         EXIT;
845  out_free_memmd:
846         obd_free_memmd(ll_i2dtexp(dir), &lsm);
847  out:
848         return rc;
849 }
850
851 static int ll_unlink_raw(struct nameidata *nd)
852 {
853         struct inode *dir = nd->dentry->d_inode;
854         struct ptlrpc_request *request = NULL;
855         struct mdc_op_data *op_data;
856         int rc;
857         ENTRY;
858         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
859                nd->last.len, nd->last.name, dir->i_ino, dir->i_generation, dir);
860
861         OBD_ALLOC(op_data, sizeof(*op_data));
862         if (op_data == NULL)
863                 RETURN(-ENOMEM);
864         ll_prepare_mdc_data(op_data, dir, NULL, nd->last.name, nd->last.len, 0);
865         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
866         OBD_FREE(op_data, sizeof(*op_data));
867         if (rc)
868                 GOTO(out, rc);
869         ll_update_times(request, 0, dir);
870         
871         rc = ll_objects_destroy(request, dir, 2);
872         EXIT;
873 out:
874         ptlrpc_req_finished(request);
875         return rc;
876 }
877
878 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
879 {
880         struct inode *src = srcnd->dentry->d_inode;
881         struct inode *tgt = tgtnd->dentry->d_inode;
882         struct ptlrpc_request *request = NULL;
883         struct ll_sb_info *sbi = ll_i2sbi(src);
884         struct mdc_op_data *op_data;
885         int err;
886         ENTRY;
887         
888         CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
889                "tgt_dir=%lu/%u(%p)\n", srcnd->last.len, srcnd->last.name,
890                src->i_ino, src->i_generation, src, tgtnd->last.len,
891                tgtnd->last.name, tgt->i_ino, tgt->i_generation, tgt);
892
893         OBD_ALLOC(op_data, sizeof(*op_data));
894         if (op_data == NULL)
895                 RETURN(-ENOMEM);
896         ll_prepare_mdc_data(op_data, src, tgt, NULL, 0, 0);
897         err = md_rename(sbi->ll_md_exp, op_data, srcnd->last.name, 
898                         srcnd->last.len, tgtnd->last.name, tgtnd->last.len, 
899                         &request);
900         OBD_FREE(op_data, sizeof(*op_data));
901         if (!err) {
902                 ll_update_times(request, 0, src);
903                 ll_update_times(request, 0, tgt);
904                 err = ll_objects_destroy(request, src, 3);
905         }
906
907         ptlrpc_req_finished(request);
908         RETURN(err);
909 }
910
911 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
912 #define LLITE_IT_RAWOPS (IT_MKNOD|IT_MKDIR|IT_SYMLINK|IT_LINK|IT_UNLINK|IT_RMDIR|IT_RENAME)
913 static int ll_rawop_from_intent(struct nameidata *nd)
914 {
915         int error = 0;
916
917         if (!nd || !(nd->intent.open.op & LLITE_IT_RAWOPS))
918                 return 0;
919
920         switch (nd->intent.open.op) {
921         case IT_MKNOD:
922                 error = ll_mknod_raw(nd, nd->intent.open.create_mode,
923                                      nd->intent.open.create.dev);
924                 break;
925         case IT_MKDIR:
926                 error = ll_mkdir_raw(nd, nd->intent.open.create_mode);
927                 break;
928         case IT_RMDIR:
929                 error = ll_rmdir_raw(nd);
930                 break;
931         case IT_UNLINK:
932                 error = ll_unlink_raw(nd);
933                 break;
934         case IT_SYMLINK:
935                 LASSERT(nd->intent.open.create.link);
936                 error = ll_symlink_raw(nd, nd->intent.open.create.link);
937                 break;
938         case IT_LINK:
939                 error = ll_link_raw(nd->intent.open.create.source_nd, nd);
940                 break;
941         case IT_RENAME:
942                 LASSERT(nd->intent.open.create.source_nd);
943                 error = ll_rename_raw(nd->intent.open.create.source_nd, nd);
944                 break;
945         default:
946                 LBUG();
947         }
948         if (error != -EOPNOTSUPP)
949                 nd->intent.open.flags |= IT_STATUS_RAW;
950
951         return error;
952 }
953 #endif
954
955 struct inode_operations ll_dir_inode_operations = {
956         .mknod              = ll_mknod,
957         .setattr            = ll_setattr,
958 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
959         .create_it          = ll_create_it,
960         .lookup_it          = ll_lookup_it,
961         .revalidate_it      = ll_inode_revalidate_it,
962 #else
963         .lookup             = ll_lookup_nd,
964         .create             = ll_create_nd,
965         .getattr            = ll_getattr,
966         .endparentlookup    = ll_rawop_from_intent,
967 #endif
968         .setxattr           = ll_setxattr,
969         .getxattr           = ll_getxattr,
970         .listxattr          = ll_listxattr,
971         .removexattr        = ll_removexattr,
972         .permission         = ll_inode_permission,
973 };