Whamcloud - gitweb
d8ec0d1fffb2ac8a5ff1c0fad8ca6b0053111316
[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 "llite_internal.h"
54
55 typedef struct ext2_dir_entry_2 ext2_dirent;
56
57 #define PageChecked(page)        test_bit(PG_checked, &(page)->flags)
58 #define SetPageChecked(page)     set_bit(PG_checked, &(page)->flags)
59
60 /* returns the page unlocked, but with a reference */
61 static int ll_dir_readpage(struct file *file, struct page *page)
62 {
63         struct inode *inode = page->mapping->host;
64         struct ptlrpc_request *request;
65         struct mds_body *body;
66         struct lustre_id id;
67         __u64 offset;
68         int rc = 0;
69         ENTRY;
70
71         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
72                inode->i_generation, inode);
73
74         mdc_pack_id(&id, inode->i_ino, inode->i_generation, 
75                     S_IFDIR, id_group(&ll_i2info(inode)->lli_id),
76                     id_fid(&ll_i2info(inode)->lli_id));
77
78         offset = page->index << PAGE_SHIFT;
79         rc = md_readpage(ll_i2sbi(inode)->ll_lmv_exp, &id, offset,
80                          page, &request);
81         if (!rc) {
82                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
83                 LASSERT (body != NULL);          /* checked by md_readpage() */
84                 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
85
86 #warning "FIXME ASAP!"
87                 //inode->i_size = body->size;
88                 SetPageUptodate(page);
89         }
90         ptlrpc_req_finished(request);
91
92         unlock_page(page);
93         RETURN(rc);
94 }
95
96 struct address_space_operations ll_dir_aops = {
97         .readpage  = ll_dir_readpage,
98 };
99
100 /*
101  * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
102  * more robust, but we have what we have
103  */
104 static inline unsigned ext2_chunk_size(struct inode *inode)
105 {
106         return inode->i_sb->s_blocksize;
107 }
108
109 static inline void ext2_put_page(struct page *page)
110 {
111         kunmap(page);
112         page_cache_release(page);
113 }
114
115 static inline unsigned long dir_pages(struct inode *inode)
116 {
117         return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
118 }
119
120 static void ext2_check_page(struct page *page)
121 {
122         struct inode *dir = page->mapping->host;
123         unsigned chunk_size = ext2_chunk_size(dir);
124         char *kaddr = page_address(page);
125         //      u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
126         unsigned offs, rec_len;
127         unsigned limit = PAGE_CACHE_SIZE;
128         ext2_dirent *p;
129         char *error;
130
131         if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
132                 limit = dir->i_size & ~PAGE_CACHE_MASK;
133                 if (limit & (chunk_size - 1)) {
134                         CERROR("limit %d dir size %lld index %ld\n",
135                                limit, dir->i_size, page->index);
136                         goto Ebadsize;
137                 }
138                 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
139                         ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
140                         p->rec_len = cpu_to_le16(chunk_size);
141                         p->name_len = 0;
142                         p->inode = 0;
143                 }
144                 if (!limit)
145                         goto out;
146         }
147         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
148                 p = (ext2_dirent *)(kaddr + offs);
149                 rec_len = le16_to_cpu(p->rec_len);
150
151                 if (rec_len < EXT2_DIR_REC_LEN(1))
152                         goto Eshort;
153                 if (rec_len & 3)
154                         goto Ealign;
155                 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
156                         goto Enamelen;
157                 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
158                         goto Espan;
159                 //              if (le32_to_cpu(p->inode) > max_inumber)
160                 //goto Einumber;
161         }
162         if (offs != limit)
163                 goto Eend;
164 out:
165         SetPageChecked(page);
166         return;
167
168         /* Too bad, we had an error */
169
170 Ebadsize:
171         CERROR("ext2_check_page"
172                 "size of directory #%lu is not a multiple of chunk size\n",
173                 dir->i_ino
174         );
175         goto fail;
176 Eshort:
177         error = "rec_len is smaller than minimal";
178         goto bad_entry;
179 Ealign:
180         error = "unaligned directory entry";
181         goto bad_entry;
182 Enamelen:
183         error = "rec_len is too small for name_len";
184         goto bad_entry;
185 Espan:
186         error = "directory entry across blocks";
187         goto bad_entry;
188         //Einumber:
189         // error = "inode out of bounds";
190 bad_entry:
191         CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
192                 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
193                 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
194                 (unsigned long) le32_to_cpu(p->inode),
195                 rec_len, p->name_len);
196         goto fail;
197 Eend:
198         p = (ext2_dirent *)(kaddr + offs);
199         CERROR("ext2_check_page"
200                 "entry in directory #%lu spans the page boundary"
201                 "offset=%lu, inode=%lu",
202                 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
203                 (unsigned long) le32_to_cpu(p->inode));
204 fail:
205         SetPageChecked(page);
206         SetPageError(page);
207 }
208
209 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
210 {
211         struct ll_inode_info *li = ll_i2info(dir);
212         struct ldlm_res_id res_id =
213                 { .name = { id_fid(&li->lli_id), id_group(&li->lli_id)} };
214         struct lustre_handle lockh;
215         struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_lmv_exp);
216         struct address_space *mapping = dir->i_mapping;
217         struct page *page;
218         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
219         int rc;
220
221         obddev = md_get_real_obd(ll_i2sbi(dir)->ll_lmv_exp, NULL, 0);
222         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
223                              &res_id, LDLM_IBITS, &policy, LCK_PR, &lockh);
224         if (!rc) {
225                 struct lookup_intent it = { .it_op = IT_READDIR };
226                 struct ptlrpc_request *request;
227                 struct mdc_op_data *op_data;
228
229                 OBD_ALLOC(op_data, sizeof(*op_data));
230                 if (op_data == NULL)
231                         RETURN(ERR_PTR(-ENOMEM));
232
233                 ll_prepare_mdc_data(op_data, dir, NULL, NULL, 0, 0);
234
235                 rc = md_enqueue(ll_i2sbi(dir)->ll_lmv_exp, LDLM_IBITS, &it,
236                                 LCK_PR, op_data, &lockh, NULL, 0,
237                                 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
238                 OBD_FREE(op_data, sizeof(*op_data));
239
240                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
241                 if (request)
242                         ptlrpc_req_finished(request);
243                 if (rc < 0) {
244                         CERROR("lock enqueue: rc: %d\n", rc);
245                         return ERR_PTR(rc);
246                 }
247         }
248         ldlm_lock_dump_handle(D_OTHER, &lockh);
249
250         page = read_cache_page(mapping, n,
251                                (filler_t*)mapping->a_ops->readpage, NULL);
252         if (!IS_ERR(page)) {
253                 wait_on_page(page);
254                 (void)kmap(page);
255                 if (!PageUptodate(page))
256                         goto fail;
257                 if (!PageChecked(page))
258                         ext2_check_page(page);
259                 if (PageError(page))
260                         goto fail;
261         }
262
263 out_unlock:
264         ldlm_lock_decref(&lockh, LCK_PR);
265         return page;
266
267 fail:
268         ext2_put_page(page);
269         page = ERR_PTR(-EIO);
270         goto out_unlock;
271 }
272
273 /*
274  * p is at least 6 bytes before the end of page
275  */
276 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
277 {
278         return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
279 }
280
281 static inline unsigned
282 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
283 {
284         ext2_dirent *de = (ext2_dirent*)(base + offset);
285         ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
286         while ((char*)p < (char*)de)
287                 p = ext2_next_entry(p);
288         return (char *)p - base;
289 }
290
291 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
292         [EXT2_FT_UNKNOWN]       DT_UNKNOWN,
293         [EXT2_FT_REG_FILE]      DT_REG,
294         [EXT2_FT_DIR]           DT_DIR,
295         [EXT2_FT_CHRDEV]        DT_CHR,
296         [EXT2_FT_BLKDEV]        DT_BLK,
297         [EXT2_FT_FIFO]          DT_FIFO,
298         [EXT2_FT_SOCK]          DT_SOCK,
299         [EXT2_FT_SYMLINK]       DT_LNK,
300 };
301
302
303 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
304 {
305         struct inode *inode = filp->f_dentry->d_inode;
306         loff_t pos = filp->f_pos;
307         // XXX struct super_block *sb = inode->i_sb;
308         unsigned offset = pos & ~PAGE_CACHE_MASK;
309         unsigned long n = pos >> PAGE_CACHE_SHIFT;
310         unsigned long npages = dir_pages(inode);
311         unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
312         unsigned char *types = ext2_filetype_table;
313         int need_revalidate = (filp->f_version != inode->i_version);
314         int rc = 0;
315         ENTRY;
316
317         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
318                inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
319
320         if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
321                 RETURN(0);
322
323         for ( ; n < npages; n++, offset = 0) {
324                 char *kaddr, *limit;
325                 ext2_dirent *de;
326                 struct page *page;
327
328                 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
329                        PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
330                        n, npages, inode->i_size);
331                 page = ll_get_dir_page(inode, n);
332
333                 /* size might have been updated by md_readpage() */
334                 npages = dir_pages(inode);
335
336                 if (IS_ERR(page)) {
337                         rc = PTR_ERR(page);
338                         CERROR("error reading dir %lu/%u page %lu: rc %d\n",
339                                inode->i_ino, inode->i_generation, n, rc);
340                         continue;
341                 }
342
343                 kaddr = page_address(page);
344                 if (need_revalidate) {
345                         offset = ext2_validate_entry(kaddr, offset, chunk_mask);
346                         need_revalidate = 0;
347                 }
348                 de = (ext2_dirent *)(kaddr+offset);
349                 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
350                 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
351                         if (de->inode) {
352                                 int over;
353                                 
354                                 rc = 0; /* no error if we return something */
355                                 
356                                 offset = (char *)de - kaddr;
357                                 over = filldir(dirent, de->name, de->name_len,
358                                                (n<<PAGE_CACHE_SHIFT) | offset,
359                                                le32_to_cpu(de->inode),
360                                                types[de->file_type &
361                                                      (EXT2_FT_MAX - 1)]);
362
363                                 if (over) {
364                                         ext2_put_page(page);
365                                         GOTO(done, rc);
366                                 }
367                         }
368                 }
369                 ext2_put_page(page);
370         }
371
372         EXIT;
373 done:
374         filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
375         filp->f_version = inode->i_version;
376         update_atime(inode);
377         return rc;
378 }
379
380 static int ll_mkdir_stripe(struct inode *inode, unsigned long arg)
381 {
382         struct ptlrpc_request *request = NULL;
383         struct ll_sb_info *sbi = ll_i2sbi(inode);
384         struct ll_user_mkdir_stripe lums;
385         struct mdc_op_data *op_data;
386         u16 nstripes;
387         mode_t mode;
388         char *name;
389         int err = 0;
390         ENTRY;
391
392         if (copy_from_user(&lums, (void *)arg, sizeof(lums)))
393                 RETURN(-EFAULT);
394
395         if (lums.lums_namelen <= 0)
396                 RETURN(-EINVAL);
397         OBD_ALLOC(name, lums.lums_namelen);
398         if (!name)
399                 RETURN(-ENOMEM);
400
401         if (copy_from_user(name, lums.lums_name, lums.lums_namelen))
402                 GOTO(out, err = -EFAULT);
403
404         CDEBUG(D_VFSTRACE, "ioctl Op:name=%s,dir=%lu/%u(%p)\n",
405                name, inode->i_ino, inode->i_generation, inode);
406         nstripes = lums.lums_nstripes;
407
408         mode = lums.lums_mode;
409         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
410
411         OBD_ALLOC(op_data, sizeof(*op_data));
412         if (op_data == NULL)
413                 GOTO(out, err = -ENOMEM);
414         ll_prepare_mdc_data(op_data, inode, NULL, name,lums.lums_namelen,0);
415         err = md_create(sbi->ll_lmv_exp, op_data, &nstripes, sizeof(nstripes),
416                         mode, current->fsuid, current->fsgid, 0, &request);
417         OBD_FREE(op_data, sizeof(*op_data));
418         ptlrpc_req_finished(request);
419         EXIT;
420 out:
421         OBD_FREE(name, lums.lums_namelen);
422         return err;
423 }
424
425 static int ll_dir_ioctl(struct inode *inode, struct file *file,
426                         unsigned int cmd, unsigned long arg)
427 {
428         struct ll_sb_info *sbi = ll_i2sbi(inode);
429         struct obd_ioctl_data *data;
430         ENTRY;
431
432         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
433                inode->i_ino, inode->i_generation, inode, cmd);
434
435         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
436                 return -ENOTTY;
437
438         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
439         switch(cmd) {
440         case EXT3_IOC_GETFLAGS:
441         case EXT3_IOC_SETFLAGS:
442                 RETURN(ll_iocontrol(inode, file, cmd, arg));
443         case IOC_MDC_LOOKUP: {
444                 struct ptlrpc_request *request = NULL;
445                 struct lustre_id id;
446                 char *buf = NULL;
447                 char *filename;
448                 int namelen, rc, len = 0;
449                 __u64 valid;
450
451                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
452                 if (rc)
453                         RETURN(rc);
454                 data = (void *)buf;
455
456                 filename = data->ioc_inlbuf1;
457                 namelen = data->ioc_inllen1;
458
459                 if (namelen < 1) {
460                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
461                         GOTO(out, rc = -EINVAL);
462                 }
463
464                 valid = OBD_MD_FLID;
465                 ll_inode2id(&id, inode);
466                 rc = md_getattr_lock(sbi->ll_lmv_exp, &id,
467                                      filename, namelen, valid, 0, &request);
468                 if (rc < 0) {
469                         CDEBUG(D_INFO, "md_getattr_lock: %d\n", rc);
470                         GOTO(out, rc);
471                 }
472
473                 ptlrpc_req_finished(request);
474
475                 EXIT;
476         out:
477                 obd_ioctl_freedata(buf, len);
478                 return rc;
479         }
480         case LL_IOC_MDC_MKDIRSTRIPE:
481                 RETURN(ll_mkdir_stripe(inode, arg));
482         case IOC_MDC_FINISH_GNS:
483                 RETURN(ll_finish_gns(sbi));
484         case LL_IOC_LOV_SETSTRIPE: {
485                 struct ptlrpc_request *request = NULL;
486                 struct mdc_op_data *op_data;
487                 struct iattr attr = { 0 };
488                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
489                 int rc = 0;
490
491                 LASSERT(sizeof(lum) == sizeof(*lump));
492                 LASSERT(sizeof(lum.lmm_objects[0]) ==
493                         sizeof(lump->lmm_objects[0]));
494                 rc = copy_from_user(&lum, lump, sizeof(lum));
495                 if (rc)
496                         RETURN(-EFAULT);
497
498                 if (lum.lmm_magic != LOV_USER_MAGIC)
499                         RETURN(-EINVAL);
500
501                 OBD_ALLOC(op_data, sizeof(*op_data));
502                 if (op_data == NULL)
503                         RETURN(-ENOMEM);
504                 
505                 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
506
507                 rc = md_setattr(sbi->ll_lmv_exp, op_data, &attr, &lum,
508                                 sizeof(lum), NULL, 0, &request);
509                 OBD_FREE(op_data, sizeof(*op_data));
510                 ptlrpc_req_finished(request);
511
512                 if (rc) {
513                         if (rc != -EPERM && rc != -EACCES)
514                                 CERROR("md_setattr fails: rc = %d\n", rc);
515                 }
516                 RETURN(rc);
517         }
518         case LL_IOC_LOV_GETSTRIPE: {
519                 struct ptlrpc_request *request = NULL;
520                 struct lov_user_md *lump = (struct lov_user_md *)arg;
521                 struct lov_mds_md *lmm;
522                 struct lustre_id id;
523                 struct mds_body *body;
524                 __u64 valid = 0;
525                 int rc, lmmsize;
526
527                 valid |= OBD_MD_FLDIREA;
528
529                 ll_inode2id(&id, inode);
530                 rc = md_getattr(sbi->ll_lmv_exp, &id, valid,
531                                 obd_size_diskmd(sbi->ll_lov_exp, NULL),
532                                 &request);
533                 if (rc < 0) {
534                         CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
535                         RETURN(rc);
536                 }
537
538                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
539                 LASSERT(body != NULL);         /* checked by md_getattr_lock */
540                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
541
542                 lmmsize = body->eadatasize;
543                 if (lmmsize == 0)
544                         GOTO(out_get, rc = -ENODATA);
545
546                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
547                 LASSERT(lmm != NULL);
548                 LASSERT_REPSWABBED(request, 1);
549                 rc = copy_to_user(lump, lmm, lmmsize);
550                 if (rc)
551                         GOTO(out_get, rc = -EFAULT);
552
553                 EXIT;
554         out_get:
555                 ptlrpc_req_finished(request);
556                 RETURN(rc);
557         }
558         case IOC_MDC_SHOWFID: {
559                 struct lustre_id *idp = (struct lustre_id *)arg;
560                 struct lustre_id id;
561                 char *filename;
562                 int rc;
563
564                 filename = getname((const char *)arg);
565                 if (IS_ERR(filename))
566                         RETURN(PTR_ERR(filename));
567
568                 ll_inode2id(&id, inode);
569
570                 rc = ll_get_fid(sbi->ll_lmv_exp, &id, filename, &id);
571                 if (rc < 0)
572                         GOTO(out_filename, rc);
573
574                 rc = copy_to_user(idp, &id, sizeof(*idp));
575                 if (rc)
576                         GOTO(out_filename, rc = -EFAULT);
577
578                 EXIT;
579         out_filename:
580                 putname(filename);
581                 return rc;
582         }
583         case IOC_MDC_GETSTRIPE: {
584                 struct lov_user_md *lump = (struct lov_user_md *)arg;
585                 struct ptlrpc_request *request = NULL;
586                 struct lov_mds_md *lmm;
587                 struct mds_body *body;
588                 struct lustre_id id;
589                 char *filename;
590                 int rc, lmmsize;
591
592                 filename = getname((const char *)arg);
593                 if (IS_ERR(filename))
594                         RETURN(PTR_ERR(filename));
595
596                 ll_inode2id(&id, inode);
597                 rc = md_getattr_lock(sbi->ll_lmv_exp, &id, filename,
598                                      strlen(filename) + 1, OBD_MD_FLEASIZE,
599                                      obd_size_diskmd(sbi->ll_lov_exp, NULL),
600                                      &request);
601                 if (rc < 0) {
602                         CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
603                                filename, rc);
604                         GOTO(out_name, rc);
605                 }
606
607                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
608                 LASSERT(body != NULL);         /* checked by md_getattr_lock */
609                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
610
611                 lmmsize = body->eadatasize;
612
613                 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
614                         GOTO(out_req, rc = -ENODATA);
615
616                 if (lmmsize > 4096)
617                         GOTO(out_req, rc = -EFBIG);
618
619                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
620                 LASSERT(lmm != NULL);
621                 LASSERT_REPSWABBED(request, 1);
622
623                 rc = copy_to_user(lump, lmm, lmmsize);
624                 if (rc)
625                         GOTO(out_req, rc = -EFAULT);
626
627                 EXIT;
628         out_req:
629                 ptlrpc_req_finished(request);
630         out_name:
631                 putname(filename);
632                 return rc;
633         }
634         case OBD_IOC_PING: {
635                 struct ptlrpc_request *req = NULL;
636                 char *buf = NULL;
637                 int rc, len=0;
638                 struct client_obd *cli;
639                 struct obd_device *obd;
640
641                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
642                 if (rc)
643                         RETURN(rc);
644                 data = (void *)buf;
645
646                 obd = class_name2obd(data->ioc_inlbuf1);
647
648                 if (!obd )
649                         GOTO(out_ping, rc = -ENODEV);
650
651                 if (!obd->obd_attached) {
652                         CERROR("Device %d not attached\n", obd->obd_minor);
653                         GOTO(out_ping, rc = -ENODEV);
654                 }
655                 if (!obd->obd_set_up) {
656                         CERROR("Device %d still not setup\n", obd->obd_minor);
657                         GOTO(out_ping, rc = -ENODEV);
658                 }
659                 cli = &obd->u.cli;
660                 req = ptlrpc_prep_req(cli->cl_import, LUSTRE_OBD_VERSION,
661                                       OBD_PING, 0, NULL, NULL);
662                 if (!req)
663                         GOTO(out_ping, rc = -ENOMEM);
664
665                 req->rq_replen = lustre_msg_size(0, NULL);
666                 req->rq_send_state = LUSTRE_IMP_FULL;
667
668                 rc = ptlrpc_queue_wait(req);
669
670                 ptlrpc_req_finished(req);
671         out_ping:
672                 obd_ioctl_freedata(buf, len);
673                 return rc;
674         }
675         case OBD_IOC_LLOG_CATINFO: {
676                 struct ptlrpc_request *req = NULL;
677                 char *buf = NULL;
678                 int rc, len = 0;
679                 char *bufs[2], *str;
680                 int lens[2], size;
681
682                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
683                 if (rc)
684                         RETURN(rc);
685                 data = (void *)buf;
686
687                 if (!data->ioc_inlbuf1) {
688                         obd_ioctl_freedata(buf, len);
689                         RETURN(-EINVAL);
690                 }
691
692                 lens[0] = data->ioc_inllen1;
693                 bufs[0] = data->ioc_inlbuf1;
694                 if (data->ioc_inllen2) {
695                         lens[1] = data->ioc_inllen2;
696                         bufs[1] = data->ioc_inlbuf2;
697                 } else {
698                         lens[1] = 0;
699                         bufs[1] = NULL;
700                 }
701                 size = data->ioc_plen1;
702                 req = ptlrpc_prep_req(sbi2lmv(sbi)->cl_import,
703                                       LUSTRE_LOG_VERSION, LLOG_CATINFO,
704                                       2, lens, bufs);
705                 if (!req)
706                         GOTO(out_catinfo, rc = -ENOMEM);
707
708                 req->rq_replen = lustre_msg_size(1, &size);
709
710                 rc = ptlrpc_queue_wait(req);
711                 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
712                 if (!rc)
713                         rc = copy_to_user(data->ioc_pbuf1, str,
714                                           data->ioc_plen1);
715                 ptlrpc_req_finished(req);
716         out_catinfo:
717                 obd_ioctl_freedata(buf, len);
718                 RETURN(rc);
719         }
720         default:
721                 return obd_iocontrol(cmd, sbi->ll_lov_exp, 0,
722                                      NULL, (void *)arg);
723         }
724 }
725
726 int ll_dir_open(struct inode *inode, struct file *file)
727 {
728         ENTRY;
729         RETURN(ll_file_open(inode, file));
730 }
731
732 int ll_dir_release(struct inode *inode, struct file *file)
733 {
734         ENTRY;
735         RETURN(ll_file_release(inode, file));
736 }
737
738 struct file_operations ll_dir_operations = {
739         .open     = ll_dir_open,
740         .release  = ll_dir_release,
741         .read     = generic_read_dir,
742         .readdir  = ll_readdir,
743         .ioctl    = ll_dir_ioctl
744 };
745