Whamcloud - gitweb
- many fixes in readdir proto.
[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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
37 # include <linux/locks.h>   // for wait_on_buffer
38 #else
39 # include <linux/buffer_head.h>   // for wait_on_buffer
40 #endif
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <lustre_lib.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre_lite.h>
49 #include <lustre_dlm.h>
50 #include "llite_internal.h"
51
52 #define PageChecked(page)        test_bit(PG_checked, &(page)->flags)
53 #define SetPageChecked(page)     set_bit(PG_checked, &(page)->flags)
54
55 /* returns the page unlocked, but with a reference */
56 static int ll_dir_readpage(struct file *file, struct page *page)
57 {
58         struct inode *inode = page->mapping->host;
59         struct ptlrpc_request *request;
60         struct mdt_body *body;
61         __u64 offset;
62         int rc = 0;
63         ENTRY;
64
65         offset = (__u64)page->index << PAGE_SHIFT;
66         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off "LPU64"\n",
67                inode->i_ino, inode->i_generation, inode, offset);
68
69         rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode),
70                          offset, page, &request);
71         if (!rc) {
72                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
73                 LASSERT (body != NULL);         /* checked by md_readpage() */
74                 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
75
76                 if (body->valid & OBD_MD_FLSIZE)
77                         inode->i_size = body->size;
78                 SetPageUptodate(page);
79         }
80         ptlrpc_req_finished(request);
81
82         unlock_page(page);
83         EXIT;
84         return rc;
85 }
86
87 struct address_space_operations ll_dir_aops = {
88         .readpage  = ll_dir_readpage,
89 };
90
91 static inline unsigned long dir_pages(struct inode *inode)
92 {
93         return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
94 }
95
96 static inline unsigned ll_chunk_size(struct inode *inode)
97 {
98         return inode->i_sb->s_blocksize;
99 }
100
101 static void ll_check_page(struct inode *dir, struct page *page)
102 {
103         /* XXX: check page format later */
104         SetPageChecked(page);
105 }
106
107 static inline void ll_put_page(struct page *page)
108 {
109         kunmap(page);
110         page_cache_release(page);
111 }
112
113 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
114 {
115         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
116         struct address_space *mapping = dir->i_mapping;
117         struct lustre_handle lockh;
118         struct page *page;
119         int rc;
120
121         rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
122                            ll_inode2fid(dir), LDLM_IBITS, &policy, LCK_CR, &lockh);
123         if (!rc) {
124                 struct lookup_intent it = { .it_op = IT_READDIR };
125                 struct md_op_data op_data = { { 0 } };
126                 struct ptlrpc_request *request;
127
128                 ll_prepare_md_op_data(&op_data, dir, NULL, NULL, 0, 0);
129
130                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, LDLM_IBITS, &it,
131                                 LCK_CR, &op_data, &lockh, NULL, 0,
132                                 ldlm_completion_ast, ll_md_blocking_ast, dir,
133                                 0);
134
135                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
136                 if (request)
137                         ptlrpc_req_finished(request);
138                 if (rc < 0) {
139                         CERROR("lock enqueue: rc: %d\n", rc);
140                         return ERR_PTR(rc);
141                 }
142         }
143         ldlm_lock_dump_handle(D_OTHER, &lockh);
144
145         page = read_cache_page(mapping, n,
146                                (filler_t*)mapping->a_ops->readpage, NULL);
147         if (IS_ERR(page))
148                 GOTO(out_unlock, page);
149
150         wait_on_page(page);
151         (void)kmap(page);
152         if (!PageUptodate(page))
153                 goto fail;
154         if (!PageChecked(page))
155                 ll_check_page(dir, page);
156         if (PageError(page))
157                 goto fail;
158
159 out_unlock:
160         ldlm_lock_decref(&lockh, LCK_CR);
161         return page;
162
163 fail:
164         ll_put_page(page);
165         page = ERR_PTR(-EIO);
166         goto out_unlock;
167 }
168
169 static inline struct lu_dir_entry *ll_next_entry(struct lu_dir_entry *p)
170 {
171         return (struct lu_dir_entry *)((char *)p + le16_to_cpu(p->de_rec_len));
172 }
173
174 int ll_readdir(struct file *filp, void *dirent, filldir_t filldir)
175 {
176         struct inode *inode = filp->f_dentry->d_inode;
177         loff_t pos = filp->f_pos;
178         unsigned offset = pos & ~PAGE_CACHE_MASK;
179         unsigned long n = pos >> PAGE_CACHE_SHIFT;
180         unsigned long npages = dir_pages(inode);
181         int rc = 0;
182         ENTRY;
183
184         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
185                inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
186
187         if (pos > inode->i_size - LU_DIR_REC_LEN(1))
188                 RETURN(0);
189
190         for ( ; n < npages; n++, offset = 0) {
191                 char *kaddr, *limit;
192                 struct lu_dir_entry *de;
193                 struct page *page;
194
195                 CDEBUG(D_VFSTRACE,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
196                        PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
197                        n, npages, inode->i_size);
198                 page = ll_get_dir_page(inode, n);
199
200                 /* size might have been updated by md_readpage */
201                 npages = dir_pages(inode);
202
203                 if (IS_ERR(page)) {
204                         rc = PTR_ERR(page);
205                         CERROR("error reading dir %lu/%u page %lu: rc %d\n",
206                                inode->i_ino, inode->i_generation, n, rc);
207                         continue;
208                 }
209
210                 kaddr = page_address(page);
211                 
212                 de = (struct lu_dir_entry *)(kaddr + offset);
213                 limit = kaddr + PAGE_CACHE_SIZE - LU_DIR_REC_LEN(1);
214                 for ( ;(char*)de <= limit; de = ll_next_entry(de)) {
215                         if (fid_oid(&de->de_fid) && fid_seq(&de->de_fid)) {
216                                 struct ll_sb_info *sbi = ll_i2sbi(inode);
217                                 int over;
218
219                                 rc = 0; /* no error if we return something */
220
221                                 offset = (char *)de - kaddr;
222                                 fid_le_to_cpu(&de->de_fid);
223                                 over = filldir(dirent, de->de_name, de->de_name_len,
224                                                (n << PAGE_CACHE_SHIFT) | offset,
225                                                ll_fid_build_ino(sbi, &de->de_fid),
226                                                0);
227                                 if (over) {
228                                         ll_put_page(page);
229                                         GOTO(done, rc);
230                                 }
231                         }
232                 }
233                 ll_put_page(page);
234         }
235
236 done:
237         filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
238         filp->f_version = inode->i_version;
239         touch_atime(filp->f_vfsmnt, filp->f_dentry);
240
241         RETURN(rc);
242 }
243
244 #define QCTL_COPY(out, in)              \
245 do {                                    \
246         Q_COPY(out, in, qc_cmd);        \
247         Q_COPY(out, in, qc_type);       \
248         Q_COPY(out, in, qc_id);         \
249         Q_COPY(out, in, qc_stat);       \
250         Q_COPY(out, in, qc_dqinfo);     \
251         Q_COPY(out, in, qc_dqblk);      \
252 } while (0)
253
254 static int ll_dir_ioctl(struct inode *inode, struct file *file,
255                         unsigned int cmd, unsigned long arg)
256 {
257         struct ll_sb_info *sbi = ll_i2sbi(inode);
258         struct obd_ioctl_data *data;
259         ENTRY;
260
261         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
262                inode->i_ino, inode->i_generation, inode, cmd);
263
264         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
265         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
266                 return -ENOTTY;
267
268         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
269         switch(cmd) {
270         case EXT3_IOC_GETFLAGS:
271         case EXT3_IOC_SETFLAGS:
272                 RETURN(ll_iocontrol(inode, file, cmd, arg));
273         case EXT3_IOC_GETVERSION_OLD:
274         case EXT3_IOC_GETVERSION:
275                 RETURN(put_user(inode->i_generation, (int *)arg));
276         /* We need to special case any other ioctls we want to handle,
277          * to send them to the MDS/OST as appropriate and to properly
278          * network encode the arg field.
279         case EXT3_IOC_SETVERSION_OLD:
280         case EXT3_IOC_SETVERSION:
281         */
282         case IOC_MDC_LOOKUP: {
283                 struct ptlrpc_request *request = NULL;
284                 int namelen, rc, len = 0;
285                 char *buf = NULL;
286                 char *filename;
287
288                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
289                 if (rc)
290                         RETURN(rc);
291                 data = (void *)buf;
292
293                 filename = data->ioc_inlbuf1;
294                 namelen = data->ioc_inllen1;
295
296                 if (namelen < 1) {
297                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
298                         GOTO(out, rc = -EINVAL);
299                 }
300
301                 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
302                                      filename, namelen, OBD_MD_FLID, 0,
303                                      &request);
304                 if (rc < 0) {
305                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
306                         GOTO(out, rc);
307                 }
308
309                 ptlrpc_req_finished(request);
310
311                 EXIT;
312         out:
313                 obd_ioctl_freedata(buf, len);
314                 return rc;
315         }
316         case LL_IOC_LOV_SETSTRIPE: {
317                 struct ptlrpc_request *request = NULL;
318                 struct md_op_data op_data = { { 0 } };
319                 struct iattr attr = { 0 };
320                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
321                 int rc = 0;
322
323                 ll_prepare_md_op_data(&op_data, inode,
324                                       NULL, NULL, 0, 0);
325
326                 LASSERT(sizeof(lum) == sizeof(*lump));
327                 LASSERT(sizeof(lum.lmm_objects[0]) ==
328                         sizeof(lump->lmm_objects[0]));
329                 rc = copy_from_user(&lum, lump, sizeof(lum));
330                 if (rc)
331                         return(-EFAULT);
332
333                 /*
334                  * This is coming from userspace, so should be in
335                  * local endian.  But the MDS would like it in little
336                  * endian, so we swab it before we send it.
337                  */
338                 if (lum.lmm_magic != LOV_USER_MAGIC)
339                         RETURN(-EINVAL);
340
341                 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
342                         lustre_swab_lov_user_md(&lum);
343
344                 /* swabbing is done in lov_setstripe() on server side */
345                 rc = md_setattr(sbi->ll_md_exp, &op_data,
346                                 &attr, &lum, sizeof(lum), NULL, 0, &request);
347                 if (rc) {
348                         ptlrpc_req_finished(request);
349                         if (rc != -EPERM && rc != -EACCES)
350                                 CERROR("md_setattr fails: rc = %d\n", rc);
351                         return rc;
352                 }
353                 ptlrpc_req_finished(request);
354
355                 return rc;
356         }
357         case LL_IOC_LOV_GETSTRIPE: {
358                 struct ptlrpc_request *request = NULL;
359                 struct lov_user_md *lump = (struct lov_user_md *)arg;
360                 struct lov_mds_md *lmm;
361                 struct mdt_body *body;
362                 int rc, lmmsize;
363
364                 rc = ll_get_max_mdsize(sbi, &lmmsize);
365                 if (rc)
366                         RETURN(rc);
367
368                 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
369                                 OBD_MD_FLDIREA, lmmsize, &request);
370                 if (rc < 0) {
371                         CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
372                         RETURN(rc);
373                 }
374
375                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
376                 LASSERT(body != NULL);         /* checked by md_getattr_name */
377                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_name */
378
379                 lmmsize = body->eadatasize;
380                 if (lmmsize == 0)
381                         GOTO(out_get, rc = -ENODATA);
382
383                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
384                 LASSERT(lmm != NULL);
385                 LASSERT_REPSWABBED(request, 1);
386
387                 /*
388                  * This is coming from the MDS, so is probably in
389                  * little endian.  We convert it to host endian before
390                  * passing it to userspace.
391                  */
392                 if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
393                         lustre_swab_lov_user_md((struct lov_user_md *)lmm);
394                         lustre_swab_lov_user_md_objects((struct lov_user_md *)lmm);
395                 }
396
397                 rc = copy_to_user(lump, lmm, lmmsize);
398                 if (rc)
399                         GOTO(out_get, rc = -EFAULT);
400
401                 EXIT;
402         out_get:
403                 ptlrpc_req_finished(request);
404                 return rc;
405         }
406         case LL_IOC_OBD_STATFS:
407                 RETURN(ll_obd_statfs(inode, (void *)arg));
408         case IOC_MDC_GETFILEINFO:
409         case IOC_MDC_GETSTRIPE: {
410                 struct ptlrpc_request *request = NULL;
411                 struct lov_user_md *lump;
412                 struct lov_mds_md *lmm;
413                 struct mdt_body *body;
414                 char *filename;
415                 int rc, lmmsize;
416
417                 filename = getname((const char *)arg);
418                 if (IS_ERR(filename))
419                         RETURN(PTR_ERR(filename));
420
421                 rc = ll_get_max_mdsize(sbi, &lmmsize);
422                 if (rc)
423                         RETURN(rc);
424
425                 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
426                                      filename, strlen(filename) + 1,
427                                      OBD_MD_FLEASIZE, lmmsize, &request);
428                 if (rc < 0) {
429                         CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
430                                filename, rc);
431                         GOTO(out_name, rc);
432                 }
433
434                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
435                 LASSERT(body != NULL);         /* checked by md_getattr_name */
436                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_name */
437
438                 lmmsize = body->eadatasize;
439
440                 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
441                         GOTO(out_req, rc = -ENODATA);
442
443                 if (lmmsize > 4096)
444                         GOTO(out_req, rc = -EFBIG);
445
446                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
447                 LASSERT(lmm != NULL);
448                 LASSERT_REPSWABBED(request, 1);
449
450                 /*
451                  * This is coming from the MDS, so is probably in
452                  * little endian.  We convert it to host endian before
453                  * passing it to userspace.
454                  */
455                 if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
456                         lustre_swab_lov_user_md((struct lov_user_md *)lmm);
457                         lustre_swab_lov_user_md_objects((struct lov_user_md *)lmm);
458                 } else if (lmm->lmm_magic == __swab32(LOV_MAGIC_JOIN)) {
459                         lustre_swab_lov_user_md_join((struct lov_user_md_join *)lmm);
460                 }
461                 if (lmm->lmm_magic == LOV_MAGIC_JOIN) {
462                         struct lov_stripe_md *lsm;
463                         struct lov_user_md_join *lmj;
464                         int lmj_size, i, aindex = 0, rc;
465
466                         rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
467                         if (rc < 0)
468                                 GOTO(out_req, rc = -ENOMEM);
469                         rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
470                         if (rc)
471                                 GOTO(out_free_memmd, rc);
472
473                         lmj_size = sizeof(struct lov_user_md_join) +
474                                    lsm->lsm_stripe_count *
475                                    sizeof(struct lov_user_ost_data_join);
476                         OBD_ALLOC(lmj, lmj_size);
477                         if (!lmj)
478                                 GOTO(out_free_memmd, rc = -ENOMEM);
479
480                         memcpy(lmj, lmm, sizeof(struct lov_user_md_join));
481                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
482                                 struct lov_array_info *lai = lsm->lsm_array;
483                                 if ((lai->lai_ext_array[aindex].le_loi_idx +
484                                      lai->lai_ext_array[aindex].le_stripe_count)<=i){
485                                         aindex ++;
486                                 }
487                                 CDEBUG(D_INFO, "aindex %d i %d l_extent_start"
488                                        LPU64"len %d \n", aindex, i,
489                                        lai->lai_ext_array[aindex].le_start,
490                                        (int)lai->lai_ext_array[aindex].le_len);
491                                 lmj->lmm_objects[i].l_extent_start =
492                                         lai->lai_ext_array[aindex].le_start;
493
494                                 if ((int)lai->lai_ext_array[aindex].le_len == -1) {
495                                         lmj->lmm_objects[i].l_extent_end = -1;
496                                 } else {
497                                         lmj->lmm_objects[i].l_extent_end =
498                                           lai->lai_ext_array[aindex].le_start +
499                                           lai->lai_ext_array[aindex].le_len;
500                                 }
501                                 lmj->lmm_objects[i].l_object_id =
502                                         lsm->lsm_oinfo[i].loi_id;
503                                 lmj->lmm_objects[i].l_object_gr =
504                                         lsm->lsm_oinfo[i].loi_gr;
505                                 lmj->lmm_objects[i].l_ost_gen =
506                                         lsm->lsm_oinfo[i].loi_ost_gen;
507                                 lmj->lmm_objects[i].l_ost_idx =
508                                         lsm->lsm_oinfo[i].loi_ost_idx;
509                         }
510                         lmm = (struct lov_mds_md *)lmj;
511                         lmmsize = lmj_size;
512 out_free_memmd:
513                         obd_free_memmd(sbi->ll_dt_exp, &lsm);
514                         if (rc)
515                                 GOTO(out_req, rc);
516                 }
517                 if (cmd == IOC_MDC_GETFILEINFO) {
518                         struct lov_user_mds_data *lmdp;
519                         lstat_t st = { 0 };
520
521                         st.st_dev     = 0;
522                         st.st_mode    = body->mode;
523                         st.st_nlink   = body->nlink;
524                         st.st_uid     = body->uid;
525                         st.st_gid     = body->gid;
526                         st.st_rdev    = body->rdev;
527                         st.st_size    = body->size;
528                         st.st_blksize = PAGE_SIZE;
529                         st.st_blocks  = body->blocks;
530                         st.st_atime   = body->atime;
531                         st.st_mtime   = body->mtime;
532                         st.st_ctime   = body->ctime;
533                         st.st_ino     = inode->i_ino;
534
535                         lmdp = (struct lov_user_mds_data *)arg;
536                         rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
537                         if (rc)
538                                 GOTO(out_req, rc = -EFAULT);
539                         lump = &lmdp->lmd_lmm;
540                 } else {
541                         lump = (struct lov_user_md *)arg;
542                 }
543
544                 rc = copy_to_user(lump, lmm, lmmsize);
545                 if (lmm->lmm_magic == LOV_MAGIC_JOIN)
546                         OBD_FREE(lmm, lmmsize);
547                 if (rc)
548                         GOTO(out_req, rc = -EFAULT);
549
550                 EXIT;
551         out_req:
552                 ptlrpc_req_finished(request);
553         out_name:
554                 putname(filename);
555                 return rc;
556         }
557         case OBD_IOC_LLOG_CATINFO: {
558                 struct ptlrpc_request *req = NULL;
559                 char *buf = NULL;
560                 int rc, len = 0;
561                 char *bufs[2], *str;
562                 int lens[2], size;
563
564                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
565                 if (rc)
566                         RETURN(rc);
567                 data = (void *)buf;
568
569                 if (!data->ioc_inlbuf1) {
570                         obd_ioctl_freedata(buf, len);
571                         RETURN(-EINVAL);
572                 }
573
574                 lens[0] = data->ioc_inllen1;
575                 bufs[0] = data->ioc_inlbuf1;
576                 if (data->ioc_inllen2) {
577                         lens[1] = data->ioc_inllen2;
578                         bufs[1] = data->ioc_inlbuf2;
579                 } else {
580                         lens[1] = 0;
581                         bufs[1] = NULL;
582                 }
583                 size = data->ioc_plen1;
584                 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import,
585                                       LUSTRE_LOG_VERSION, LLOG_CATINFO,
586                                       2, lens, bufs);
587                 if (!req)
588                         GOTO(out_catinfo, rc = -ENOMEM);
589                 req->rq_replen = lustre_msg_size(1, &size);
590
591                 rc = ptlrpc_queue_wait(req);
592                 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
593                 if (!rc)
594                         rc = copy_to_user(data->ioc_pbuf1, str,
595                                           data->ioc_plen1);
596                 ptlrpc_req_finished(req);
597         out_catinfo:
598                 obd_ioctl_freedata(buf, len);
599                 RETURN(rc);
600         }
601         case OBD_IOC_QUOTACHECK: {
602                 struct obd_quotactl *oqctl;
603                 int rc, error = 0;
604
605                 if (!capable(CAP_SYS_ADMIN))
606                         RETURN(-EPERM);
607
608                 OBD_ALLOC_PTR(oqctl);
609                 if (!oqctl)
610                         RETURN(-ENOMEM);
611                 oqctl->qc_type = arg;
612                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
613                 if (rc < 0) {
614                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
615                         error = rc;
616                 }
617
618                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
619                 if (rc < 0)
620                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
621
622                 OBD_FREE_PTR(oqctl);
623                 return error ?: rc;
624         }
625         case OBD_IOC_POLL_QUOTACHECK: {
626                 struct if_quotacheck *check;
627                 int rc;
628
629                 if (!capable(CAP_SYS_ADMIN))
630                         RETURN(-EPERM);
631
632                 OBD_ALLOC_PTR(check);
633                 if (!check)
634                         RETURN(-ENOMEM);
635
636                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
637                                    NULL);
638                 if (rc) {
639                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
640                         if (copy_to_user((void *)arg, check, sizeof(*check)))
641                                 rc = -EFAULT;
642                         GOTO(out_poll, rc);
643                 }
644
645                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
646                                    NULL);
647                 if (rc) {
648                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
649                         if (copy_to_user((void *)arg, check, sizeof(*check)))
650                                 rc = -EFAULT;
651                         GOTO(out_poll, rc);
652                 }
653         out_poll:
654                 OBD_FREE_PTR(check);
655                 RETURN(rc);
656         }
657 #if HAVE_QUOTA_SUPPORT
658         case OBD_IOC_QUOTACTL: {
659                 struct if_quotactl *qctl;
660                 struct obd_quotactl *oqctl;
661
662                 int cmd, type, id, rc = 0;
663
664                 OBD_ALLOC_PTR(qctl);
665                 if (!qctl)
666                         RETURN(-ENOMEM);
667
668                 OBD_ALLOC_PTR(oqctl);
669                 if (!oqctl) {
670                         OBD_FREE_PTR(qctl);
671                         RETURN(-ENOMEM);
672                 }
673                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
674                         GOTO(out_quotactl, rc = -EFAULT);
675
676                 cmd = qctl->qc_cmd;
677                 type = qctl->qc_type;
678                 id = qctl->qc_id;
679                 switch (cmd) {
680                 case Q_QUOTAON:
681                 case Q_QUOTAOFF:
682                 case Q_SETQUOTA:
683                 case Q_SETINFO:
684                         if (!capable(CAP_SYS_ADMIN))
685                                 GOTO(out_quotactl, rc = -EPERM);
686                         break;
687                 case Q_GETQUOTA:
688                         if (((type == USRQUOTA && current->euid != id) ||
689                              (type == GRPQUOTA && !in_egroup_p(id))) &&
690                             !capable(CAP_SYS_ADMIN))
691                                 GOTO(out_quotactl, rc = -EPERM);
692
693                         /* XXX: dqb_valid is borrowed as a flag to mark that
694                          *      only mds quota is wanted */
695                         if (qctl->qc_dqblk.dqb_valid)
696                                 qctl->obd_uuid = sbi->ll_md_exp->exp_obd->
697                                                         u.cli.cl_target_uuid;
698                         break;
699                 case Q_GETINFO:
700                         break;
701                 default:
702                         CERROR("unsupported quotactl op: %#x\n", cmd);
703                         GOTO(out_quotactl, -ENOTTY);
704                 }
705
706                 QCTL_COPY(oqctl, qctl);
707
708                 if (qctl->obd_uuid.uuid[0]) {
709                         struct obd_device *obd;
710                         struct obd_uuid *uuid = &qctl->obd_uuid;
711
712                         obd = class_find_client_notype(uuid,
713                                          &sbi->ll_dt_exp->exp_obd->obd_uuid);
714                         if (!obd)
715                                 GOTO(out_quotactl, rc = -ENOENT);
716
717                         if (cmd == Q_GETINFO)
718                                 oqctl->qc_cmd = Q_GETOINFO;
719                         else if (cmd == Q_GETQUOTA)
720                                 oqctl->qc_cmd = Q_GETOQUOTA;
721                         else
722                                 GOTO(out_quotactl, rc = -EINVAL);
723
724                         if (sbi->ll_md_exp->exp_obd == obd) {
725                                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
726                         } else {
727                                 int i;
728                                 struct obd_export *exp;
729                                 struct lov_obd *lov = &sbi->ll_dt_exp->
730                                                             exp_obd->u.lov;
731
732                                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
733                                         exp = lov->tgts[i].ltd_exp;
734
735                                         if (!lov->tgts[i].active)
736                                                 continue;
737
738                                         if (exp->exp_obd == obd) {
739                                                 rc = obd_quotactl(exp, oqctl);
740                                                 break;
741                                         }
742                                 }
743                         }
744
745                         oqctl->qc_cmd = cmd;
746                         QCTL_COPY(qctl, oqctl);
747
748                         if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
749                                 rc = -EFAULT;
750
751                         GOTO(out_quotactl, rc);
752                 }
753
754                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
755                 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
756                         oqctl->qc_cmd = Q_QUOTAOFF;
757                         obd_quotactl(sbi->ll_md_exp, oqctl);
758                 }
759
760                 QCTL_COPY(qctl, oqctl);
761
762                 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
763                         rc = -EFAULT;
764         out_quotactl:
765                 OBD_FREE_PTR(qctl);
766                 OBD_FREE_PTR(oqctl);
767                 RETURN(rc);
768         }
769 #endif /* HAVE_QUOTA_SUPPORT */
770         case OBD_IOC_GETNAME: {
771                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
772                 if (!obd)
773                         RETURN(-EFAULT);
774                 if (copy_to_user((void *)arg, obd->obd_name,
775                                 strlen(obd->obd_name) + 1))
776                         RETURN (-EFAULT);
777                 RETURN(0);
778         }
779         default:
780                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
781         }
782 }
783
784 int ll_dir_open(struct inode *inode, struct file *file)
785 {
786         ENTRY;
787         RETURN(ll_file_open(inode, file));
788 }
789
790 int ll_dir_release(struct inode *inode, struct file *file)
791 {
792         ENTRY;
793         RETURN(ll_file_release(inode, file));
794 }
795
796 struct file_operations ll_dir_operations = {
797         .open     = ll_dir_open,
798         .release  = ll_dir_release,
799         .read     = generic_read_dir,
800         .readdir  = ll_readdir,
801         .ioctl    = ll_dir_ioctl
802 };
803