Whamcloud - gitweb
55e6e798910d29ac5b67b4c9fa7afa1f61de83c4
[fs/lustre-release.git] / lustre / llite / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  from
10  *
11  *  linux/fs/minix/dir.c
12  *  linux/fs/ext2/dir.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  ext2 directory handling functions
17  *
18  *  Big-endian to little-endian byte-swapping/bitmaps by
19  *        David S. Miller (davem@caip.rutgers.edu), 1995
20  *
21  *  All code that works with directory layout had been switched to pagecache
22  *  and moved here. AV
23  *
24  *  Adapted for Lustre Light
25  *  Copyright (C) 2002-2003, Cluster File Systems, Inc.
26  *
27  */
28
29 #include <linux/fs.h>
30 #include <linux/ext2_fs.h>
31 #include <linux/pagemap.h>
32 #include <linux/mm.h>
33 #include <linux/version.h>
34 #include <linux/smp_lock.h>
35 #include <asm/uaccess.h>
36 #include <linux/file.h>
37 #include <linux/kmod.h>
38 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
39 # include <linux/locks.h>   // for wait_on_buffer
40 #else
41 # include <linux/buffer_head.h>   // for wait_on_buffer
42 #endif
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <linux/obd_support.h>
47 #include <linux/obd_class.h>
48 #include <linux/lustre_lib.h>
49 #include <linux/lustre_idl.h>
50 #include <linux/lustre_mds.h>
51 #include <linux/lustre_lite.h>
52 #include <linux/lustre_dlm.h>
53 #include <linux/lustre_sec.h>
54 #include <linux/lustre_acl.h>
55 #include "llite_internal.h"
56
57 typedef struct ext2_dir_entry_2 ext2_dirent;
58
59 #define PageChecked(page)        test_bit(PG_checked, &(page)->flags)
60 #define SetPageChecked(page)     set_bit(PG_checked, &(page)->flags)
61
62 /* returns the page unlocked, but with a reference */
63 static int ll_dir_readpage(struct file *file, struct page *page)
64 {
65         struct inode *inode = page->mapping->host;
66         struct ptlrpc_request *request;
67         struct mds_body *body;
68         struct lustre_id id;
69         __u64 offset;
70         int rc = 0;
71         ENTRY;
72
73         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
74                inode->i_generation, inode);
75
76         mdc_pack_id(&id, inode->i_ino, inode->i_generation, 
77                     S_IFDIR, id_group(&ll_i2info(inode)->lli_id),
78                     id_fid(&ll_i2info(inode)->lli_id));
79
80         offset = page->index << PAGE_SHIFT;
81         rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, &id, offset,
82                          page, &request);
83         if (!rc) {
84                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
85                 LASSERT (body != NULL);          /* checked by md_readpage() */
86                 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
87
88 #warning "FIXME ASAP!"
89                 //inode->i_size = body->size;
90                 SetPageUptodate(page);
91         }
92         ptlrpc_req_finished(request);
93
94         unlock_page(page);
95         RETURN(rc);
96 }
97
98 struct address_space_operations ll_dir_aops = {
99         .readpage  = ll_dir_readpage,
100 };
101
102 /*
103  * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
104  * more robust, but we have what we have
105  */
106 static inline unsigned ext2_chunk_size(struct inode *inode)
107 {
108         return inode->i_sb->s_blocksize;
109 }
110
111 static inline void ext2_put_page(struct page *page)
112 {
113         kunmap(page);
114         page_cache_release(page);
115 }
116
117 static inline unsigned long dir_pages(struct inode *inode)
118 {
119         return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
120 }
121
122 static void ext2_check_page(struct page *page)
123 {
124         struct inode *dir = page->mapping->host;
125         unsigned chunk_size = ext2_chunk_size(dir);
126         char *kaddr = page_address(page);
127         //      u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
128         unsigned offs, rec_len;
129         unsigned limit = PAGE_CACHE_SIZE;
130         ext2_dirent *p;
131         char *error;
132
133         if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
134                 limit = dir->i_size & ~PAGE_CACHE_MASK;
135                 if (limit & (chunk_size - 1)) {
136                         CERROR("limit %d dir size %lld index %ld\n",
137                                limit, dir->i_size, page->index);
138                         goto Ebadsize;
139                 }
140                 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
141                         ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
142                         p->rec_len = cpu_to_le16(chunk_size);
143                         p->name_len = 0;
144                         p->inode = 0;
145                 }
146                 if (!limit)
147                         goto out;
148         }
149         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
150                 p = (ext2_dirent *)(kaddr + offs);
151                 rec_len = le16_to_cpu(p->rec_len);
152
153                 if (rec_len < EXT2_DIR_REC_LEN(1))
154                         goto Eshort;
155                 if (rec_len & 3)
156                         goto Ealign;
157                 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
158                         goto Enamelen;
159                 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
160                         goto Espan;
161                 //              if (le32_to_cpu(p->inode) > max_inumber)
162                 //goto Einumber;
163         }
164         if (offs != limit)
165                 goto Eend;
166 out:
167         SetPageChecked(page);
168         return;
169
170         /* Too bad, we had an error */
171
172 Ebadsize:
173         CERROR("ext2_check_page"
174                 "size of directory #%lu is not a multiple of chunk size\n",
175                 dir->i_ino
176         );
177         goto fail;
178 Eshort:
179         error = "rec_len is smaller than minimal";
180         goto bad_entry;
181 Ealign:
182         error = "unaligned directory entry";
183         goto bad_entry;
184 Enamelen:
185         error = "rec_len is too small for name_len";
186         goto bad_entry;
187 Espan:
188         error = "directory entry across blocks";
189         goto bad_entry;
190         //Einumber:
191         // error = "inode out of bounds";
192 bad_entry:
193         CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
194                 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
195                 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
196                 (unsigned long) le32_to_cpu(p->inode),
197                 rec_len, p->name_len);
198         goto fail;
199 Eend:
200         p = (ext2_dirent *)(kaddr + offs);
201         CERROR("ext2_check_page"
202                 "entry in directory #%lu spans the page boundary"
203                 "offset=%lu, inode=%lu",
204                 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
205                 (unsigned long) le32_to_cpu(p->inode));
206 fail:
207         SetPageChecked(page);
208         SetPageError(page);
209 }
210
211 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
212 {
213         struct ll_inode_info *li = ll_i2info(dir);
214         struct ldlm_res_id res_id =
215                 { .name = { id_fid(&li->lli_id), id_group(&li->lli_id)} };
216         struct lustre_handle lockh;
217         struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_md_exp);
218         struct address_space *mapping = dir->i_mapping;
219         struct page *page;
220         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
221         int rc;
222
223         obddev = md_get_real_obd(ll_i2sbi(dir)->ll_md_exp, &li->lli_id);
224         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
225                              &res_id, LDLM_IBITS, &policy, LCK_PR, &lockh);
226         if (!rc) {
227                 struct lookup_intent it = { .it_op = IT_READDIR };
228                 struct ptlrpc_request *request;
229                 struct mdc_op_data *op_data;
230
231                 OBD_ALLOC(op_data, sizeof(*op_data));
232                 if (op_data == NULL)
233                         return ERR_PTR(-ENOMEM);
234
235                 ll_prepare_mdc_data(op_data, dir, NULL, NULL, 0, 0);
236
237                 rc = ll_intent_alloc(&it);
238                 if (rc)
239                         return ERR_PTR(rc);
240
241                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, LDLM_IBITS, &it,
242                                 LCK_PR, op_data, &lockh, NULL, 0,
243                                 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
244                 OBD_FREE(op_data, sizeof(*op_data));
245
246                 request = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
247                 ll_intent_free(&it);
248
249                 if (request)
250                         ptlrpc_req_finished(request);
251                 if (rc < 0) {
252                         CERROR("lock enqueue: rc: %d\n", rc);
253                         return ERR_PTR(rc);
254                 }
255         } else {
256                 if (ptlrpcs_check_cred(obddev->u.cli.cl_import)) {
257                         /* return immediately if no credential held */
258                         ldlm_lock_decref(&lockh, LCK_PR);
259                         return ERR_PTR(-EACCES);
260                 }
261         }
262         ldlm_lock_dump_handle(D_OTHER, &lockh);
263
264         page = read_cache_page(mapping, n,
265                                (filler_t*)mapping->a_ops->readpage, NULL);
266         if (!IS_ERR(page)) {
267                 wait_on_page(page);
268                 (void)kmap(page);
269                 if (!PageUptodate(page))
270                         goto fail;
271                 if (!PageChecked(page))
272                         ext2_check_page(page);
273                 if (PageError(page))
274                         goto fail;
275         }
276
277 out_unlock:
278         ldlm_lock_decref(&lockh, LCK_PR);
279         return page;
280
281 fail:
282         ext2_put_page(page);
283         page = ERR_PTR(-EIO);
284         goto out_unlock;
285 }
286
287 /*
288  * p is at least 6 bytes before the end of page
289  */
290 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
291 {
292         return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
293 }
294
295 static inline unsigned
296 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
297 {
298         ext2_dirent *de = (ext2_dirent*)(base + offset);
299         ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
300         while ((char*)p < (char*)de)
301                 p = ext2_next_entry(p);
302         return (char *)p - base;
303 }
304
305 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
306         [EXT2_FT_UNKNOWN]       DT_UNKNOWN,
307         [EXT2_FT_REG_FILE]      DT_REG,
308         [EXT2_FT_DIR]           DT_DIR,
309         [EXT2_FT_CHRDEV]        DT_CHR,
310         [EXT2_FT_BLKDEV]        DT_BLK,
311         [EXT2_FT_FIFO]          DT_FIFO,
312         [EXT2_FT_SOCK]          DT_SOCK,
313         [EXT2_FT_SYMLINK]       DT_LNK,
314 };
315
316
317 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
318 {
319         struct inode *inode = filp->f_dentry->d_inode;
320         loff_t pos = filp->f_pos;
321         // XXX struct super_block *sb = inode->i_sb;
322         unsigned offset = pos & ~PAGE_CACHE_MASK;
323         unsigned long n = pos >> PAGE_CACHE_SHIFT;
324         unsigned long npages = dir_pages(inode);
325         unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
326         unsigned char *types = ext2_filetype_table;
327         int need_revalidate = (filp->f_version != inode->i_version);
328         int rc = 0;
329         ENTRY;
330
331         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
332                inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
333
334         if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
335                 RETURN(0);
336
337         for ( ; n < npages; n++, offset = 0) {
338                 char *kaddr, *limit;
339                 ext2_dirent *de;
340                 struct page *page;
341
342                 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
343                        PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
344                        n, npages, inode->i_size);
345                 page = ll_get_dir_page(inode, n);
346
347                 /* size might have been updated by md_readpage() */
348                 npages = dir_pages(inode);
349
350                 if (IS_ERR(page)) {
351                         rc = PTR_ERR(page);
352                         CERROR("error reading dir %lu/%u page %lu: rc %d\n",
353                                inode->i_ino, inode->i_generation, n, rc);
354                         continue;
355                 }
356
357                 kaddr = page_address(page);
358                 if (need_revalidate) {
359                         offset = ext2_validate_entry(kaddr, offset, chunk_mask);
360                         need_revalidate = 0;
361                 }
362                 de = (ext2_dirent *)(kaddr+offset);
363                 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
364                 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
365                         if (de->inode) {
366                                 int over;
367                                 
368                                 rc = 0; /* no error if we return something */
369                                 
370                                 offset = (char *)de - kaddr;
371                                 over = filldir(dirent, de->name, de->name_len,
372                                                (n<<PAGE_CACHE_SHIFT) | offset,
373                                                le32_to_cpu(de->inode),
374                                                types[de->file_type &
375                                                      (EXT2_FT_MAX - 1)]);
376
377                                 if (over) {
378                                         ext2_put_page(page);
379                                         GOTO(done, rc);
380                                 }
381                         }
382                 }
383                 ext2_put_page(page);
384         }
385
386         EXIT;
387 done:
388         filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
389         filp->f_version = inode->i_version;
390         update_atime(inode);
391         return rc;
392 }
393
394 static int ll_mkdir_stripe(struct inode *inode, unsigned long arg)
395 {
396         struct ptlrpc_request *request = NULL;
397         struct ll_sb_info *sbi = ll_i2sbi(inode);
398         struct ll_user_mkdir_stripe lums;
399         struct mdc_op_data *op_data;
400         u16 nstripes;
401         mode_t mode;
402         char *name;
403         int err = 0;
404         ENTRY;
405
406         if (copy_from_user(&lums, (void *)arg, sizeof(lums)))
407                 RETURN(-EFAULT);
408
409         if (lums.lums_namelen <= 0)
410                 RETURN(-EINVAL);
411         OBD_ALLOC(name, lums.lums_namelen);
412         if (!name)
413                 RETURN(-ENOMEM);
414
415         if (copy_from_user(name, lums.lums_name, lums.lums_namelen))
416                 GOTO(out, err = -EFAULT);
417
418         CDEBUG(D_VFSTRACE, "ioctl Op:name=%s,dir=%lu/%u(%p)\n",
419                name, inode->i_ino, inode->i_generation, inode);
420         nstripes = lums.lums_nstripes;
421
422         mode = lums.lums_mode;
423         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
424
425         OBD_ALLOC(op_data, sizeof(*op_data));
426         if (op_data == NULL)
427                 GOTO(out, err = -ENOMEM);
428         ll_prepare_mdc_data(op_data, inode, NULL, name,lums.lums_namelen,0);
429         err = md_create(sbi->ll_md_exp, op_data, &nstripes, sizeof(nstripes),
430                         mode, current->fsuid, current->fsgid, 0, &request);
431         OBD_FREE(op_data, sizeof(*op_data));
432         ptlrpc_req_finished(request);
433         EXIT;
434 out:
435         OBD_FREE(name, lums.lums_namelen);
436         return err;
437 }
438
439 /*
440  * we don't call getxattr_internal/setxattr_internal because we
441  * need more precisely control.
442  */
443 static int ll_ioctl_getfacl(struct inode *inode,
444                             struct file *file,
445                             struct ll_acl_ioctl_data *ioc)
446 {
447         struct ptlrpc_request *req = NULL;
448         struct mds_body *body;
449         char *cmd, *res;
450         struct lustre_id id;
451         int rc;
452         ENTRY;
453
454         if (!ioc->cmd || !ioc->cmd_len ||
455             !ioc->res || !ioc->res_len) {
456                 CERROR("error: cmd %p, len %lu, res %p, len %lu\n",
457                        ioc->cmd, ioc->cmd_len, ioc->res, ioc->res_len);
458                 RETURN(-EINVAL);
459         }
460
461         OBD_ALLOC(cmd, ioc->cmd_len);
462         if (!cmd)
463                 RETURN(-ENOMEM);
464         if (copy_from_user(cmd, ioc->cmd, ioc->cmd_len))
465                 GOTO(out, rc = -EFAULT);
466
467         /* we didn't call ll_getxattr_internal() because we'd like to
468          * copy from reply buffer to user space directly.
469          */
470         ll_inode2id(&id, inode);
471         rc = md_getattr(ll_i2sbi(inode)->ll_md_exp, &id, OBD_MD_FLXATTR,
472                         XATTR_NAME_LUSTRE_ACL,
473                         cmd, ioc->cmd_len, ioc->res_len, &req);
474         if (rc < 0) {
475                 CERROR("rc: %d\n", rc);
476                 GOTO(out, rc);
477         }
478
479         res = lustre_msg_buf(req->rq_repmsg, 1, ioc->res_len);
480         LASSERT(res);
481         if (copy_to_user(ioc->res, res, ioc->res_len))
482                 rc = -EFAULT;
483
484         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
485         LASSERT(body);
486         ioc->status = (__s32) body->flags;
487
488         EXIT;
489 out:
490         if (req)
491                 ptlrpc_req_finished(req);
492         OBD_FREE(cmd, ioc->cmd_len);
493
494         return rc;
495 }
496
497 static int ll_ioctl_setfacl(struct inode *inode,
498                             struct file *file,
499                             struct ll_acl_ioctl_data *ioc)
500 {
501         struct ptlrpc_request *req = NULL;
502         struct mdc_op_data op_data;
503         struct mds_body *body;
504         struct iattr attr;
505         char *cmd;
506         int replen, rc;
507         ENTRY;
508
509         if (!ioc->cmd || !ioc->cmd_len) {
510                 CERROR("error: cmd %p, len %lu\n", ioc->cmd, ioc->cmd_len);
511                 RETURN(-EINVAL);
512         }
513
514         OBD_ALLOC(cmd, ioc->cmd_len);
515         if (!cmd)
516                 RETURN(-ENOMEM);
517         if (copy_from_user(cmd, ioc->cmd, ioc->cmd_len))
518                 GOTO(out, rc = -EFAULT);
519
520         memset(&attr, 0x0, sizeof(attr));
521         attr.ia_valid |= ATTR_EA;
522         attr.ia_attr_flags = 0;
523
524         ll_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
525
526         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, &op_data, &attr,
527                         (void*) XATTR_NAME_LUSTRE_ACL,
528                         sizeof(XATTR_NAME_LUSTRE_ACL),
529                         (void*) cmd, ioc->cmd_len, &req);
530         if (rc) {
531                 CERROR("md_setattr fails: rc = %d\n", rc);
532                 GOTO(out, rc);
533         }
534
535         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
536         LASSERT(body);
537         ioc->status = (__s32) body->flags;
538
539         LASSERT(req->rq_repmsg->bufcount == 2);
540         replen = req->rq_repmsg->buflens[1];
541         LASSERT(replen <= LUSTRE_ACL_SIZE_MAX);
542         if (replen) {
543                 if (replen > ioc->res_len)
544                         replen = ioc->res_len;
545                 if (copy_to_user(ioc->res,
546                                  lustre_msg_buf(req->rq_repmsg, 1, replen),
547                                  replen))
548                         rc = -EFAULT;
549         }
550         EXIT;
551 out:
552         if (req)
553                 ptlrpc_req_finished(req);
554         OBD_FREE(cmd, ioc->cmd_len);
555
556         return rc;
557 }
558
559 static int ll_dir_ioctl(struct inode *inode, struct file *file,
560                         unsigned int cmd, unsigned long arg)
561 {
562         struct ll_sb_info *sbi = ll_i2sbi(inode);
563         struct obd_ioctl_data *data;
564         ENTRY;
565
566         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
567                inode->i_ino, inode->i_generation, inode, cmd);
568
569         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
570                 return -ENOTTY;
571
572         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
573         switch(cmd) {
574         case EXT3_IOC_GETFLAGS:
575         case EXT3_IOC_SETFLAGS:
576                 RETURN(ll_iocontrol(inode, file, cmd, arg));
577         case IOC_MDC_LOOKUP: {
578                 struct ptlrpc_request *request = NULL;
579                 struct lustre_id id;
580                 char *buf = NULL;
581                 char *filename;
582                 int namelen, rc, len = 0;
583                 __u64 valid;
584
585                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
586                 if (rc)
587                         RETURN(rc);
588                 data = (void *)buf;
589
590                 filename = data->ioc_inlbuf1;
591                 namelen = data->ioc_inllen1;
592
593                 if (namelen < 1) {
594                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
595                         GOTO(out, rc = -EINVAL);
596                 }
597
598                 valid = OBD_MD_FLID;
599                 ll_inode2id(&id, inode);
600                 rc = md_getattr_lock(sbi->ll_md_exp, &id,
601                                      filename, namelen, valid, 0, &request);
602                 if (rc < 0) {
603                         CDEBUG(D_INFO, "md_getattr_lock: %d\n", rc);
604                         GOTO(out, rc);
605                 }
606
607                 ptlrpc_req_finished(request);
608
609                 EXIT;
610         out:
611                 obd_ioctl_freedata(buf, len);
612                 return rc;
613         }
614         case LL_IOC_MDC_MKDIRSTRIPE:
615                 RETURN(ll_mkdir_stripe(inode, arg));
616         case LL_IOC_LOV_SETSTRIPE: {
617                 struct ptlrpc_request *request = NULL;
618                 struct mdc_op_data *op_data;
619                 struct iattr attr = { 0 };
620                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
621                 int rc = 0;
622
623                 LASSERT(sizeof(lum) == sizeof(*lump));
624                 LASSERT(sizeof(lum.lmm_objects[0]) ==
625                         sizeof(lump->lmm_objects[0]));
626                 rc = copy_from_user(&lum, lump, sizeof(lum));
627                 if (rc)
628                         RETURN(-EFAULT);
629
630                 if (lum.lmm_magic != LOV_USER_MAGIC)
631                         RETURN(-EINVAL);
632
633                 OBD_ALLOC(op_data, sizeof(*op_data));
634                 if (op_data == NULL)
635                         RETURN(-ENOMEM);
636                 
637                 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
638
639                 rc = md_setattr(sbi->ll_md_exp, op_data, &attr, &lum,
640                                 sizeof(lum), NULL, 0, &request);
641                 OBD_FREE(op_data, sizeof(*op_data));
642                 ptlrpc_req_finished(request);
643
644                 if (rc) {
645                         if (rc != -EPERM && rc != -EACCES)
646                                 CERROR("md_setattr fails: rc = %d\n", rc);
647                 }
648                 RETURN(rc);
649         }
650         case LL_IOC_LOV_GETSTRIPE: {
651                 struct ptlrpc_request *request = NULL;
652                 struct lov_user_md *lump = (struct lov_user_md *)arg;
653                 struct lov_mds_md *lmm;
654                 struct lustre_id id;
655                 struct mds_body *body;
656                 __u64 valid = 0;
657                 int rc, lmmsize;
658
659                 valid |= OBD_MD_FLDIREA;
660
661                 ll_inode2id(&id, inode);
662                 rc = md_getattr(sbi->ll_md_exp, &id, valid, NULL, NULL, 0,
663                                 obd_size_diskmd(sbi->ll_dt_exp, NULL),
664                                 &request);
665                 if (rc < 0) {
666                         CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
667                         RETURN(rc);
668                 }
669
670                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
671                 LASSERT(body != NULL);         /* checked by md_getattr_lock */
672                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
673
674                 lmmsize = body->eadatasize;
675                 if (lmmsize == 0)
676                         GOTO(out_get, rc = -ENODATA);
677
678                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
679                 LASSERT(lmm != NULL);
680                 LASSERT_REPSWABBED(request, 1);
681                 rc = copy_to_user(lump, lmm, lmmsize);
682                 if (rc)
683                         GOTO(out_get, rc = -EFAULT);
684
685                 EXIT;
686         out_get:
687                 ptlrpc_req_finished(request);
688                 RETURN(rc);
689         }
690         case IOC_MDC_SHOWFID: {
691                 struct lustre_id *idp = (struct lustre_id *)arg;
692                 struct lustre_id id;
693                 char *filename;
694                 int rc;
695
696                 filename = getname((const char *)arg);
697                 if (IS_ERR(filename))
698                         RETURN(PTR_ERR(filename));
699
700                 ll_inode2id(&id, inode);
701
702                 rc = ll_get_fid(sbi->ll_md_exp, &id, filename, &id);
703                 if (rc < 0)
704                         GOTO(out_filename, rc);
705
706                 rc = copy_to_user(idp, &id, sizeof(*idp));
707                 if (rc)
708                         GOTO(out_filename, rc = -EFAULT);
709
710                 EXIT;
711         out_filename:
712                 putname(filename);
713                 return rc;
714         }
715         case IOC_MDC_GETSTRIPE: {
716                 struct lov_user_md *lump = (struct lov_user_md *)arg;
717                 struct ptlrpc_request *request = NULL;
718                 struct lov_mds_md *lmm;
719                 struct mds_body *body;
720                 struct lustre_id id;
721                 char *filename;
722                 int rc, lmmsize;
723
724                 filename = getname((const char *)arg);
725                 if (IS_ERR(filename))
726                         RETURN(PTR_ERR(filename));
727
728                 ll_inode2id(&id, inode);
729                 rc = md_getattr_lock(sbi->ll_md_exp, &id, filename,
730                                      strlen(filename) + 1, OBD_MD_FLEASIZE,
731                                      obd_size_diskmd(sbi->ll_dt_exp, NULL),
732                                      &request);
733                 if (rc < 0) {
734                         CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
735                                filename, rc);
736                         GOTO(out_name, rc);
737                 }
738
739                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
740                 LASSERT(body != NULL);         /* checked by md_getattr_lock */
741                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
742
743                 lmmsize = body->eadatasize;
744
745                 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
746                         GOTO(out_req, rc = -ENODATA);
747
748                 if (lmmsize > 4096)
749                         GOTO(out_req, rc = -EFBIG);
750
751                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
752                 LASSERT(lmm != NULL);
753                 LASSERT_REPSWABBED(request, 1);
754
755                 rc = copy_to_user(lump, lmm, lmmsize);
756                 if (rc)
757                         GOTO(out_req, rc = -EFAULT);
758
759                 EXIT;
760         out_req:
761                 ptlrpc_req_finished(request);
762         out_name:
763                 putname(filename);
764                 return rc;
765         }
766         case OBD_IOC_PING: {
767                 struct ptlrpc_request *req = NULL;
768                 char *buf = NULL;
769                 int rc, len=0;
770                 struct client_obd *cli;
771                 struct obd_device *obd;
772
773                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
774                 if (rc)
775                         RETURN(rc);
776                 data = (void *)buf;
777
778                 obd = class_name2obd(data->ioc_inlbuf1);
779
780                 if (!obd )
781                         GOTO(out_ping, rc = -ENODEV);
782
783                 if (!obd->obd_attached) {
784                         CERROR("Device %d not attached\n", obd->obd_minor);
785                         GOTO(out_ping, rc = -ENODEV);
786                 }
787                 if (!obd->obd_set_up) {
788                         CERROR("Device %d still not setup\n", obd->obd_minor);
789                         GOTO(out_ping, rc = -ENODEV);
790                 }
791                 cli = &obd->u.cli;
792                 req = ptlrpc_prep_req(cli->cl_import, LUSTRE_OBD_VERSION,
793                                       OBD_PING, 0, NULL, NULL);
794                 if (!req)
795                         GOTO(out_ping, rc = -ENOMEM);
796
797                 req->rq_replen = lustre_msg_size(0, NULL);
798                 req->rq_send_state = LUSTRE_IMP_FULL;
799
800                 rc = ptlrpc_queue_wait(req);
801
802                 ptlrpc_req_finished(req);
803         out_ping:
804                 obd_ioctl_freedata(buf, len);
805                 return rc;
806         }
807         case OBD_IOC_LLOG_CATINFO: {
808                 struct ptlrpc_request *req = NULL;
809                 char *buf = NULL;
810                 int rc, len = 0;
811                 char *bufs[2], *str;
812                 int lens[2], size;
813
814                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
815                 if (rc)
816                         RETURN(rc);
817                 data = (void *)buf;
818
819                 if (!data->ioc_inlbuf1) {
820                         obd_ioctl_freedata(buf, len);
821                         RETURN(-EINVAL);
822                 }
823
824                 lens[0] = data->ioc_inllen1;
825                 bufs[0] = data->ioc_inlbuf1;
826                 if (data->ioc_inllen2) {
827                         lens[1] = data->ioc_inllen2;
828                         bufs[1] = data->ioc_inlbuf2;
829                 } else {
830                         lens[1] = 0;
831                         bufs[1] = NULL;
832                 }
833                 size = data->ioc_plen1;
834                 req = ptlrpc_prep_req(sbi2md(sbi)->cl_import,
835                                       LUSTRE_LOG_VERSION, LLOG_CATINFO,
836                                       2, lens, bufs);
837                 if (!req)
838                         GOTO(out_catinfo, rc = -ENOMEM);
839
840                 req->rq_replen = lustre_msg_size(1, &size);
841
842                 rc = ptlrpc_queue_wait(req);
843                 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
844                 if (!rc)
845                         rc = copy_to_user(data->ioc_pbuf1, str,
846                                           data->ioc_plen1);
847                 ptlrpc_req_finished(req);
848         out_catinfo:
849                 obd_ioctl_freedata(buf, len);
850                 RETURN(rc);
851         }
852         case LL_IOC_GETFACL: {
853                 struct ll_acl_ioctl_data ioc, *uioc;
854                 int rc;
855
856                 if (copy_from_user(&ioc, (void *) arg, sizeof(ioc)))
857                         RETURN(-EFAULT);
858
859                 rc = ll_ioctl_getfacl(inode, file, &ioc);
860                 if (!rc) {
861                         uioc = (struct ll_acl_ioctl_data *) arg;
862                         if (copy_to_user(&uioc->status, &ioc.status,
863                                          sizeof(ioc.status)))
864                                 rc = -EFAULT;
865                 }
866                 RETURN(rc);
867         }
868         case LL_IOC_SETFACL: {
869                 struct ll_acl_ioctl_data ioc, *uioc;
870                 int rc;
871
872                 if (copy_from_user(&ioc, (void *) arg, sizeof(ioc)))
873                         RETURN(-EFAULT);
874
875                 rc = ll_ioctl_setfacl(inode, file, &ioc);
876                 if (!rc) {
877                         uioc = (struct ll_acl_ioctl_data *) arg;
878                         if (copy_to_user(&uioc->status, &ioc.status,
879                                          sizeof(ioc.status)))
880                                 rc = -EFAULT;
881                 }
882                 RETURN(rc);
883         }
884         case LL_IOC_FLUSH_CRED:
885                 RETURN(ll_flush_cred(inode));
886         default:
887                 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0,
888                                      NULL, (void *)arg);
889         }
890 }
891
892 int ll_dir_open(struct inode *inode, struct file *file)
893 {
894         return ll_file_open(inode, file);
895 }
896
897 int ll_dir_release(struct inode *inode, struct file *file)
898 {
899         return ll_file_release(inode, file);
900 }
901
902 struct file_operations ll_dir_operations = {
903         .open     = ll_dir_open,
904         .release  = ll_dir_release,
905         .read     = generic_read_dir,
906         .readdir  = ll_readdir,
907         .ioctl    = ll_dir_ioctl
908 };
909