Whamcloud - gitweb
LU-12137 llite: use ->iterate_shared() for readdir
[fs/lustre-release.git] / lustre / llite / dir.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/dir.c
33  *
34  * Directory code for lustre client.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/pagemap.h>
39 #include <linux/mm.h>
40 #include <linux/version.h>
41 #include <linux/security.h>
42 #include <linux/user_namespace.h>
43 #ifdef HAVE_UIDGID_HEADER
44 # include <linux/uidgid.h>
45 #endif
46 #include <linux/uaccess.h>
47 #include <linux/buffer_head.h>   // for wait_on_buffer
48 #include <linux/pagevec.h>
49
50 #define DEBUG_SUBSYSTEM S_LLITE
51
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include <lustre_lib.h>
56 #include <lustre_dlm.h>
57 #include <lustre_fid.h>
58 #include <lustre_kernelcomm.h>
59 #include <lustre_swab.h>
60
61 #include "llite_internal.h"
62
63 /*
64  * (new) readdir implementation overview.
65  *
66  * Original lustre readdir implementation cached exact copy of raw directory
67  * pages on the client. These pages were indexed in client page cache by
68  * logical offset in the directory file. This design, while very simple and
69  * intuitive had some inherent problems:
70  *
71  *     . it implies that byte offset to the directory entry serves as a
72  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
73  *     ext3/htree directory entries may move due to splits, and more
74  *     importantly,
75  *
76  *     . it is incompatible with the design of split directories for cmd3,
77  *     that assumes that names are distributed across nodes based on their
78  *     hash, and so readdir should be done in hash order.
79  *
80  * New readdir implementation does readdir in hash order, and uses hash of a
81  * file name as a telldir/seekdir cookie. This led to number of complications:
82  *
83  *     . hash is not unique, so it cannot be used to index cached directory
84  *     pages on the client (note, that it requires a whole pageful of hash
85  *     collided entries to cause two pages to have identical hashes);
86  *
87  *     . hash is not unique, so it cannot, strictly speaking, be used as an
88  *     entry cookie. ext3/htree has the same problem and lustre implementation
89  *     mimics their solution: seekdir(hash) positions directory at the first
90  *     entry with the given hash.
91  *
92  * Client side.
93  *
94  * 0. caching
95  *
96  * Client caches directory pages using hash of the first entry as an index. As
97  * noted above hash is not unique, so this solution doesn't work as is:
98  * special processing is needed for "page hash chains" (i.e., sequences of
99  * pages filled with entries all having the same hash value).
100  *
101  * First, such chains have to be detected. To this end, server returns to the
102  * client the hash of the first entry on the page next to one returned. When
103  * client detects that this hash is the same as hash of the first entry on the
104  * returned page, page hash collision has to be handled. Pages in the
105  * hash chain, except first one, are termed "overflow pages".
106  *
107  * Solution to index uniqueness problem is to not cache overflow
108  * pages. Instead, when page hash collision is detected, all overflow pages
109  * from emerging chain are immediately requested from the server and placed in
110  * a special data structure (struct ll_dir_chain). This data structure is used
111  * by ll_readdir() to process entries from overflow pages. When readdir
112  * invocation finishes, overflow pages are discarded. If page hash collision
113  * chain weren't completely processed, next call to readdir will again detect
114  * page hash collision, again read overflow pages in, process next portion of
115  * entries and again discard the pages. This is not as wasteful as it looks,
116  * because, given reasonable hash, page hash collisions are extremely rare.
117  *
118  * 1. directory positioning
119  *
120  * When seekdir(hash) is called, original
121  *
122  *
123  *
124  *
125  *
126  *
127  *
128  *
129  * Server.
130  *
131  * identification of and access to overflow pages
132  *
133  * page format
134  *
135  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
136  * a header lu_dirpage which describes the start/end hash, and whether this
137  * page is empty (contains no dir entry) or hash collide with next page.
138  * After client receives reply, several pages will be integrated into dir page
139  * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
140  * lu_dirpage for this integrated page will be adjusted. See
141  * mdc_adjust_dirpages().
142  *
143  */
144 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
145                              __u64 offset, struct ll_dir_chain *chain)
146 {
147         struct md_callback      cb_op;
148         struct page             *page;
149         int                     rc;
150
151         cb_op.md_blocking_ast = ll_md_blocking_ast;
152         rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
153         if (rc != 0)
154                 return ERR_PTR(rc);
155
156         return page;
157 }
158
159 void ll_release_page(struct inode *inode, struct page *page,
160                      bool remove)
161 {
162         kunmap(page);
163
164         /* Always remove the page for striped dir, because the page is
165          * built from temporarily in LMV layer */
166         if (inode && ll_dir_striped(inode)) {
167                 __free_page(page);
168                 return;
169         }
170
171         if (remove) {
172                 lock_page(page);
173                 if (likely(page->mapping != NULL))
174                         delete_from_page_cache(page);
175                 unlock_page(page);
176         }
177         put_page(page);
178 }
179
180 /**
181  * return IF_* type for given lu_dirent entry.
182  * IF_* flag shld be converted to particular OS file type in
183  * platform llite module.
184  */
185 static u16 ll_dirent_type_get(struct lu_dirent *ent)
186 {
187         u16 type = 0;
188         struct luda_type *lt;
189         int len = 0;
190
191         if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
192                 const unsigned align = sizeof(struct luda_type) - 1;
193
194                 len = le16_to_cpu(ent->lde_namelen);
195                 len = (len + align) & ~align;
196                 lt = (void *)ent->lde_name + len;
197                 type = IFTODT(le16_to_cpu(lt->lt_type));
198         }
199
200         return type;
201 }
202
203 #ifdef HAVE_DIR_CONTEXT
204 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
205                 struct dir_context *ctx)
206 {
207 #else
208 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
209                 void *cookie, filldir_t filldir)
210 {
211 #endif
212         struct ll_sb_info    *sbi        = ll_i2sbi(inode);
213         __u64                 pos        = *ppos;
214         bool                  is_api32 = ll_need_32bit_api(sbi);
215         bool                  is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
216         struct page          *page;
217         struct ll_dir_chain   chain;
218         bool                  done = false;
219         int                   rc = 0;
220         ENTRY;
221
222         ll_dir_chain_init(&chain);
223
224         page = ll_get_dir_page(inode, op_data, pos, &chain);
225
226         while (rc == 0 && !done) {
227                 struct lu_dirpage *dp;
228                 struct lu_dirent  *ent;
229                 __u64 hash;
230                 __u64 next;
231
232                 if (IS_ERR(page)) {
233                         rc = PTR_ERR(page);
234                         break;
235                 }
236
237                 hash = MDS_DIR_END_OFF;
238                 dp = page_address(page);
239                 for (ent = lu_dirent_start(dp); ent != NULL && !done;
240                      ent = lu_dirent_next(ent)) {
241                         __u16          type;
242                         int            namelen;
243                         struct lu_fid  fid;
244                         __u64          lhash;
245                         __u64          ino;
246
247                         hash = le64_to_cpu(ent->lde_hash);
248                         if (hash < pos) /* Skip until we find target hash */
249                                 continue;
250
251                         namelen = le16_to_cpu(ent->lde_namelen);
252                         if (namelen == 0) /* Skip dummy record */
253                                 continue;
254
255                         if (is_api32 && is_hash64)
256                                 lhash = hash >> 32;
257                         else
258                                 lhash = hash;
259                         fid_le_to_cpu(&fid, &ent->lde_fid);
260                         ino = cl_fid_build_ino(&fid, is_api32);
261                         type = ll_dirent_type_get(ent);
262                         /* For ll_nfs_get_name_filldir(), it will try to access
263                          * 'ent' through 'lde_name', so the parameter 'name'
264                          * for 'filldir()' must be part of the 'ent'. */
265 #ifdef HAVE_DIR_CONTEXT
266                         ctx->pos = lhash;
267                         done = !dir_emit(ctx, ent->lde_name, namelen, ino,
268                                          type);
269 #else
270                         done = filldir(cookie, ent->lde_name, namelen, lhash,
271                                        ino, type);
272 #endif
273                 }
274
275                 if (done) {
276                         pos = hash;
277                         ll_release_page(inode, page, false);
278                         break;
279                 }
280
281                 next = le64_to_cpu(dp->ldp_hash_end);
282                 pos = next;
283                 if (pos == MDS_DIR_END_OFF) {
284                         /*
285                          * End of directory reached.
286                          */
287                         done = 1;
288                         ll_release_page(inode, page, false);
289                 } else {
290                         /*
291                          * Normal case: continue to the next
292                          * page.
293                          */
294                         ll_release_page(inode, page,
295                                         le32_to_cpu(dp->ldp_flags) &
296                                         LDF_COLLIDE);
297                         next = pos;
298                         page = ll_get_dir_page(inode, op_data, pos,
299                                                &chain);
300                 }
301         }
302 #ifdef HAVE_DIR_CONTEXT
303         ctx->pos = pos;
304 #else
305         *ppos = pos;
306 #endif
307         ll_dir_chain_fini(&chain);
308         RETURN(rc);
309 }
310
311 #ifdef HAVE_DIR_CONTEXT
312 static int ll_iterate(struct file *filp, struct dir_context *ctx)
313 #else
314 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
315 #endif
316 {
317         struct inode            *inode  = file_inode(filp);
318         struct ll_file_data     *lfd    = LUSTRE_FPRIVATE(filp);
319         struct ll_sb_info       *sbi    = ll_i2sbi(inode);
320         int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
321         int                     api32   = ll_need_32bit_api(sbi);
322         struct md_op_data       *op_data;
323         struct lu_fid           pfid = { 0 };
324         __u64                   pos;
325         int                     rc;
326         ENTRY;
327
328         if (lfd != NULL)
329                 pos = lfd->lfd_pos;
330         else
331                 pos = 0;
332
333         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) pos/size"
334                "%lu/%llu 32bit_api %d\n", PFID(ll_inode2fid(inode)),
335                inode, (unsigned long)pos, i_size_read(inode), api32);
336
337         if (pos == MDS_DIR_END_OFF)
338                 /*
339                  * end-of-file.
340                  */
341                 GOTO(out, rc = 0);
342
343         if (unlikely(ll_dir_striped(inode))) {
344                 /*
345                  * This is only needed for striped dir to fill ..,
346                  * see lmv_read_page()
347                  */
348                 if (file_dentry(filp)->d_parent != NULL &&
349                     file_dentry(filp)->d_parent->d_inode != NULL) {
350                         __u64 ibits = MDS_INODELOCK_LOOKUP;
351                         struct inode *parent =
352                                 file_dentry(filp)->d_parent->d_inode;
353
354                         if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
355                                 pfid = *ll_inode2fid(parent);
356                 }
357
358                 /* If it can not find in cache, do lookup .. on the master
359                  * object */
360                 if (fid_is_zero(&pfid)) {
361                         rc = ll_dir_get_parent_fid(inode, &pfid);
362                         if (rc != 0)
363                                 RETURN(rc);
364                 }
365         }
366
367         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
368                                      LUSTRE_OPC_ANY, inode);
369         if (IS_ERR(op_data))
370                 GOTO(out, rc = PTR_ERR(op_data));
371
372         /* foreign dirs are browsed out of Lustre */
373         if (unlikely(op_data->op_mea1 != NULL &&
374                      op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
375                 ll_finish_md_op_data(op_data);
376                 RETURN(-ENODATA);
377         }
378
379         op_data->op_fid3 = pfid;
380
381 #ifdef HAVE_DIR_CONTEXT
382         ctx->pos = pos;
383         rc = ll_dir_read(inode, &pos, op_data, ctx);
384         pos = ctx->pos;
385 #else
386         rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
387 #endif
388         if (lfd != NULL)
389                 lfd->lfd_pos = pos;
390
391         if (pos == MDS_DIR_END_OFF) {
392                 if (api32)
393                         pos = LL_DIR_END_OFF_32BIT;
394                 else
395                         pos = LL_DIR_END_OFF;
396         } else {
397                 if (api32 && hash64)
398                         pos = pos >> 32;
399         }
400 #ifdef HAVE_DIR_CONTEXT
401         ctx->pos = pos;
402 #else
403         filp->f_pos = pos;
404 #endif
405         ll_finish_md_op_data(op_data);
406
407 out:
408         if (!rc)
409                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
410
411         RETURN(rc);
412 }
413
414 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
415 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
416 {
417         struct mgs_send_param *msp;
418         int rc = 0;
419
420         OBD_ALLOC_PTR(msp);
421         if (!msp)
422                 return -ENOMEM;
423
424         strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
425         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
426                                 sizeof(struct mgs_send_param), msp, NULL);
427         if (rc)
428                 CERROR("Failed to set parameter: %d\n", rc);
429         OBD_FREE_PTR(msp);
430
431         return rc;
432 }
433 #endif
434
435 /**
436  * Create striped directory with specified stripe(@lump)
437  *
438  * \param[in] dparent   the parent of the directory.
439  * \param[in] lump      the specified stripes.
440  * \param[in] dirname   the name of the directory.
441  * \param[in] mode      the specified mode of the directory.
442  *
443  * \retval              =0 if striped directory is being created successfully.
444  *                      <0 if the creation is failed.
445  */
446 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
447                                size_t len, const char *dirname, umode_t mode)
448 {
449         struct inode *parent = dparent->d_inode;
450         struct ptlrpc_request *request = NULL;
451         struct md_op_data *op_data;
452         struct ll_sb_info *sbi = ll_i2sbi(parent);
453         struct inode *inode = NULL;
454         struct dentry dentry = {
455                 .d_parent = dparent,
456                 .d_name = {
457                         .name = dirname,
458                         .len = strlen(dirname),
459                         .hash = ll_full_name_hash(dparent, dirname,
460                                                   strlen(dirname)),
461                 },
462         };
463         int err;
464         ENTRY;
465
466         if (unlikely(!lmv_magic_supported(lump->lum_magic)))
467                 RETURN(-EINVAL);
468
469         if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
470                 CDEBUG(D_VFSTRACE,
471                        "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
472                        PFID(ll_inode2fid(parent)), parent, dirname,
473                        (int)lump->lum_stripe_offset, lump->lum_stripe_count);
474         } else {
475                 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
476
477                 CDEBUG(D_VFSTRACE,
478                        "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
479                        PFID(ll_inode2fid(parent)), parent, dirname,
480                        lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
481         }
482
483         if (lump->lum_stripe_count > 1 &&
484             !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
485                 RETURN(-EINVAL);
486
487         if (IS_DEADDIR(parent) &&
488             !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
489                 RETURN(-ENOENT);
490
491         if (unlikely(!lmv_magic_supported(cpu_to_le32(lump->lum_magic))))
492                 lustre_swab_lmv_user_md(lump);
493
494         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
495                 mode &= ~current_umask();
496         mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
497         op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
498                                      strlen(dirname), mode, LUSTRE_OPC_MKDIR,
499                                      lump);
500         if (IS_ERR(op_data))
501                 RETURN(PTR_ERR(op_data));
502
503         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
504                 /* selinux_dentry_init_security() uses dentry->d_parent and name
505                  * to determine the security context for the file. So our fake
506                  * dentry should be real enough for this purpose. */
507                 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
508                                               &op_data->op_file_secctx_name,
509                                               &op_data->op_file_secctx,
510                                               &op_data->op_file_secctx_size);
511                 if (err < 0)
512                         GOTO(out_op_data, err);
513         }
514
515         op_data->op_cli_flags |= CLI_SET_MEA;
516         err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
517                         from_kuid(&init_user_ns, current_fsuid()),
518                         from_kgid(&init_user_ns, current_fsgid()),
519                         cfs_curproc_cap_pack(), 0, &request);
520         if (err)
521                 GOTO(out_request, err);
522
523         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
524
525         err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
526         if (err)
527                 GOTO(out_inode, err);
528
529         dentry.d_inode = inode;
530
531         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
532                 inode_lock(inode);
533                 err = security_inode_notifysecctx(inode,
534                                                   op_data->op_file_secctx,
535                                                   op_data->op_file_secctx_size);
536                 inode_unlock(inode);
537         } else {
538                 err = ll_inode_init_security(&dentry, inode, parent);
539         }
540         if (err)
541                 GOTO(out_inode, err);
542
543 out_inode:
544         if (inode != NULL)
545                 iput(inode);
546 out_request:
547         ptlrpc_req_finished(request);
548 out_op_data:
549         ll_finish_md_op_data(op_data);
550
551         return err;
552 }
553
554 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
555                      int set_default)
556 {
557         struct ll_sb_info *sbi = ll_i2sbi(inode);
558         struct md_op_data *op_data;
559         struct ptlrpc_request *req = NULL;
560         int rc = 0;
561 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
562         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
563         struct obd_device *mgc = lsi->lsi_mgc;
564 #endif
565         int lum_size;
566         ENTRY;
567
568         if (lump != NULL) {
569                 switch (lump->lmm_magic) {
570                 case LOV_USER_MAGIC_V1:
571                         lum_size = sizeof(struct lov_user_md_v1);
572                         break;
573                 case LOV_USER_MAGIC_V3:
574                         lum_size = sizeof(struct lov_user_md_v3);
575                         break;
576                 case LOV_USER_MAGIC_COMP_V1:
577                         lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
578                         break;
579                 case LMV_USER_MAGIC:
580                         if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
581                                 lustre_swab_lmv_user_md(
582                                         (struct lmv_user_md *)lump);
583                         lum_size = sizeof(struct lmv_user_md);
584                         break;
585                 case LOV_USER_MAGIC_SPECIFIC: {
586                         struct lov_user_md_v3 *v3 =
587                                 (struct lov_user_md_v3 *)lump;
588                         if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
589                                 RETURN(-EINVAL);
590                         lum_size = lov_user_md_size(v3->lmm_stripe_count,
591                                                     LOV_USER_MAGIC_SPECIFIC);
592                         break;
593                 }
594                 default:
595                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
596                                         " %#08x != %#08x nor %#08x\n",
597                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
598                                         LOV_USER_MAGIC_V3);
599                         RETURN(-EINVAL);
600                 }
601
602                 /*
603                  * This is coming from userspace, so should be in
604                  * local endian.  But the MDS would like it in little
605                  * endian, so we swab it before we send it.
606                  */
607                 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
608                     le32_to_cpu(LOV_MAGIC_MAGIC))
609                         lustre_swab_lov_user_md(lump, 0);
610         } else {
611                 lum_size = sizeof(struct lov_user_md_v1);
612         }
613
614         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
615                                      LUSTRE_OPC_ANY, NULL);
616         if (IS_ERR(op_data))
617                 RETURN(PTR_ERR(op_data));
618
619         /* swabbing is done in lov_setstripe() on server side */
620         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
621         ll_finish_md_op_data(op_data);
622         ptlrpc_req_finished(req);
623         if (rc)
624                 RETURN(rc);
625
626 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
627         /*
628          * 2.9 server has stored filesystem default stripe in ROOT xattr,
629          * and it's stored into system config for backward compatibility.
630          *
631          * In the following we use the fact that LOV_USER_MAGIC_V1 and
632          * LOV_USER_MAGIC_V3 have the same initial fields so we do not
633          * need the make the distiction between the 2 versions
634          */
635         if (set_default && mgc->u.cli.cl_mgc_mgsexp &&
636             (lump == NULL ||
637              le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V1 ||
638              le32_to_cpu(lump->lmm_magic) == LOV_USER_MAGIC_V3)) {
639                 char *param = NULL;
640                 char *buf;
641
642                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
643                 if (param == NULL)
644                         GOTO(end, rc = -ENOMEM);
645
646                 buf = param;
647                 /* Get fsname and assume devname to be -MDT0000. */
648                 snprintf(buf, MGS_PARAM_MAXLEN, "%s-MDT0000.lov",
649                          sbi->ll_fsname);
650                 buf += strlen(buf);
651
652                 /* Set root stripesize */
653                 snprintf(buf, MGS_PARAM_MAXLEN, ".stripesize=%u",
654                          lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
655                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
656                 if (rc)
657                         GOTO(end, rc);
658
659                 /* Set root stripecount */
660                 snprintf(buf, MGS_PARAM_MAXLEN, ".stripecount=%hd",
661                          lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
662                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
663                 if (rc)
664                         GOTO(end, rc);
665
666                 /* Set root stripeoffset */
667                 snprintf(buf, MGS_PARAM_MAXLEN, ".stripeoffset=%hd",
668                          lump ? le16_to_cpu(lump->lmm_stripe_offset) :
669                                 (typeof(lump->lmm_stripe_offset))(-1));
670                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
671
672 end:
673                 if (param != NULL)
674                         OBD_FREE(param, MGS_PARAM_MAXLEN);
675         }
676 #endif
677         RETURN(rc);
678 }
679
680 /**
681  * This function will be used to get default LOV/LMV/Default LMV
682  * @valid will be used to indicate which stripe it will retrieve
683  *      OBD_MD_MEA              LMV stripe EA
684  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
685  *      otherwise               Default LOV EA.
686  * Each time, it can only retrieve 1 stripe EA
687  **/
688 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
689                      struct ptlrpc_request **request, u64 valid)
690 {
691         struct ll_sb_info *sbi = ll_i2sbi(inode);
692         struct mdt_body   *body;
693         struct lov_mds_md *lmm = NULL;
694         struct ptlrpc_request *req = NULL;
695         int rc, lmm_size;
696         struct md_op_data *op_data;
697         ENTRY;
698
699         rc = ll_get_default_mdsize(sbi, &lmm_size);
700         if (rc)
701                 RETURN(rc);
702
703         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
704                                      0, lmm_size, LUSTRE_OPC_ANY,
705                                      NULL);
706         if (IS_ERR(op_data))
707                 RETURN(PTR_ERR(op_data));
708
709         op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
710         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
711         ll_finish_md_op_data(op_data);
712         if (rc < 0) {
713                 CDEBUG(D_INFO, "md_getattr failed on inode "
714                        DFID": rc %d\n", PFID(ll_inode2fid(inode)), rc);
715                 GOTO(out, rc);
716         }
717
718         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
719         LASSERT(body != NULL);
720
721         lmm_size = body->mbo_eadatasize;
722
723         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
724             lmm_size == 0) {
725                 GOTO(out, rc = -ENODATA);
726         }
727
728         lmm = req_capsule_server_sized_get(&req->rq_pill,
729                                            &RMF_MDT_MD, lmm_size);
730         LASSERT(lmm != NULL);
731
732         /*
733          * This is coming from the MDS, so is probably in
734          * little endian.  We convert it to host endian before
735          * passing it to userspace.
736          */
737         /* We don't swab objects for directories */
738         switch (le32_to_cpu(lmm->lmm_magic)) {
739         case LOV_MAGIC_V1:
740         case LOV_MAGIC_V3:
741         case LOV_MAGIC_COMP_V1:
742         case LOV_USER_MAGIC_SPECIFIC:
743                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
744                         lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
745                 break;
746         case LMV_MAGIC_V1:
747                 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
748                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
749                 break;
750         case LMV_USER_MAGIC:
751                 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
752                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
753                 break;
754         case LMV_MAGIC_FOREIGN: {
755                 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
756
757                 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
758                         __swab32s(&lfm->lfm_magic);
759                         __swab32s(&lfm->lfm_length);
760                         __swab32s(&lfm->lfm_type);
761                         __swab32s(&lfm->lfm_flags);
762                 }
763                 break;
764         }
765         default:
766                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
767                 rc = -EPROTO;
768         }
769 out:
770         *plmm = lmm;
771         *plmm_size = lmm_size;
772         *request = req;
773         return rc;
774 }
775
776 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
777 {
778         struct md_op_data       *op_data;
779         int                     rc;
780         int                     mdt_index;
781         ENTRY;
782
783         OBD_ALLOC_PTR(op_data);
784         if (op_data == NULL)
785                 RETURN(-ENOMEM);
786
787         op_data->op_flags |= MF_GET_MDT_IDX;
788         op_data->op_fid1 = *fid;
789         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
790         mdt_index = op_data->op_mds;
791         OBD_FREE_PTR(op_data);
792         if (rc < 0)
793                 RETURN(rc);
794
795         RETURN(mdt_index);
796 }
797
798 /*
799  *  Get MDT index for the inode.
800  */
801 int ll_get_mdt_idx(struct inode *inode)
802 {
803         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
804 }
805
806 /**
807  * Generic handler to do any pre-copy work.
808  *
809  * It sends a first hsm_progress (with extent length == 0) to coordinator as a
810  * first information for it that real work has started.
811  *
812  * Moreover, for a ARCHIVE request, it will sample the file data version and
813  * store it in \a copy.
814  *
815  * \return 0 on success.
816  */
817 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
818 {
819         struct ll_sb_info               *sbi = ll_s2sbi(sb);
820         struct hsm_progress_kernel       hpk;
821         int                              rc = 0;
822         int                              rc2;
823         ENTRY;
824
825         /* Forge a hsm_progress based on data from copy. */
826         hpk.hpk_fid = copy->hc_hai.hai_fid;
827         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
828         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
829         hpk.hpk_extent.length = 0;
830         hpk.hpk_flags = 0;
831         hpk.hpk_errval = 0;
832         hpk.hpk_data_version = 0;
833
834
835         /* For archive request, we need to read the current file version. */
836         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
837                 struct inode    *inode;
838                 __u64            data_version = 0;
839
840                 /* Get inode for this fid */
841                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
842                 if (IS_ERR(inode)) {
843                         hpk.hpk_flags |= HP_FLAG_RETRY;
844                         /* hpk_errval is >= 0 */
845                         hpk.hpk_errval = -PTR_ERR(inode);
846                         GOTO(progress, rc = PTR_ERR(inode));
847                 }
848
849                 /* Read current file data version */
850                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
851                 iput(inode);
852                 if (rc != 0) {
853                         CDEBUG(D_HSM, "Could not read file data version of "
854                                       DFID" (rc = %d). Archive request ("
855                                       "%#llx) could not be done.\n",
856                                       PFID(&copy->hc_hai.hai_fid), rc,
857                                       copy->hc_hai.hai_cookie);
858                         hpk.hpk_flags |= HP_FLAG_RETRY;
859                         /* hpk_errval must be >= 0 */
860                         hpk.hpk_errval = -rc;
861                         GOTO(progress, rc);
862                 }
863
864                 /* Store in the hsm_copy for later copytool use.
865                  * Always modified even if no lsm. */
866                 copy->hc_data_version = data_version;
867         }
868
869 progress:
870         /* On error, the request should be considered as completed */
871         if (hpk.hpk_errval > 0)
872                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
873
874         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
875                             &hpk, NULL);
876
877         /* Return first error */
878         RETURN(rc != 0 ? rc : rc2);
879 }
880
881 /**
882  * Generic handler to do any post-copy work.
883  *
884  * It will send the last hsm_progress update to coordinator to inform it
885  * that copy is finished and whether it was successful or not.
886  *
887  * Moreover,
888  * - for ARCHIVE request, it will sample the file data version and compare it
889  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
890  *   will be considered as failed.
891  * - for RESTORE request, it will sample the file data version and send it to
892  *   coordinator which is useful if the file was imported as 'released'.
893  *
894  * \return 0 on success.
895  */
896 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
897 {
898         struct ll_sb_info               *sbi = ll_s2sbi(sb);
899         struct hsm_progress_kernel       hpk;
900         int                              rc = 0;
901         int                              rc2;
902         ENTRY;
903
904         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
905         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
906          * initialized if copy_end was called with copy == NULL.
907          */
908
909         /* Forge a hsm_progress based on data from copy. */
910         hpk.hpk_fid = copy->hc_hai.hai_fid;
911         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
912         hpk.hpk_extent = copy->hc_hai.hai_extent;
913         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
914         hpk.hpk_errval = copy->hc_errval;
915         hpk.hpk_data_version = 0;
916
917         /* For archive request, we need to check the file data was not changed.
918          *
919          * For restore request, we need to send the file data version, this is
920          * useful when the file was created using hsm_import.
921          */
922         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
923              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
924             (copy->hc_errval == 0)) {
925                 struct inode    *inode;
926                 __u64            data_version = 0;
927
928                 /* Get lsm for this fid */
929                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
930                 if (IS_ERR(inode)) {
931                         hpk.hpk_flags |= HP_FLAG_RETRY;
932                         /* hpk_errval must be >= 0 */
933                         hpk.hpk_errval = -PTR_ERR(inode);
934                         GOTO(progress, rc = PTR_ERR(inode));
935                 }
936
937                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
938                 iput(inode);
939                 if (rc) {
940                         CDEBUG(D_HSM, "Could not read file data version. "
941                                       "Request could not be confirmed.\n");
942                         if (hpk.hpk_errval == 0)
943                                 hpk.hpk_errval = -rc;
944                         GOTO(progress, rc);
945                 }
946
947                 /* Store in the hsm_copy for later copytool use.
948                  * Always modified even if no lsm. */
949                 hpk.hpk_data_version = data_version;
950
951                 /* File could have been stripped during archiving, so we need
952                  * to check anyway. */
953                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
954                     (copy->hc_data_version != data_version)) {
955                         CDEBUG(D_HSM, "File data version mismatched. "
956                               "File content was changed during archiving. "
957                                DFID", start:%#llx current:%#llx\n",
958                                PFID(&copy->hc_hai.hai_fid),
959                                copy->hc_data_version, data_version);
960                         /* File was changed, send error to cdt. Do not ask for
961                          * retry because if a file is modified frequently,
962                          * the cdt will loop on retried archive requests.
963                          * The policy engine will ask for a new archive later
964                          * when the file will not be modified for some tunable
965                          * time */
966                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
967                         rc = -EBUSY;
968                         /* hpk_errval must be >= 0 */
969                         hpk.hpk_errval = -rc;
970                         GOTO(progress, rc);
971                 }
972
973         }
974
975 progress:
976         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
977                             &hpk, NULL);
978
979         /* Return first error */
980         RETURN(rc != 0 ? rc : rc2);
981 }
982
983
984 static int copy_and_ct_start(int cmd, struct obd_export *exp,
985                              const struct lustre_kernelcomm __user *data)
986 {
987         struct lustre_kernelcomm *lk;
988         struct lustre_kernelcomm *tmp;
989         size_t size = sizeof(*lk);
990         size_t new_size;
991         int i;
992         int rc;
993
994         /* copy data from userspace to get numbers of archive_id */
995         OBD_ALLOC(lk, size);
996         if (lk == NULL)
997                 return -ENOMEM;
998
999         if (copy_from_user(lk, data, size))
1000                 GOTO(out_lk, rc = -EFAULT);
1001
1002         if (lk->lk_flags & LK_FLG_STOP)
1003                 goto do_ioctl;
1004
1005         if (!(lk->lk_flags & LK_FLG_DATANR)) {
1006                 __u32 archive_mask = lk->lk_data_count;
1007                 int count;
1008
1009                 /* old hsm agent to old MDS */
1010                 if (!exp_connect_archive_id_array(exp))
1011                         goto do_ioctl;
1012
1013                 /* old hsm agent to new MDS */
1014                 lk->lk_flags |= LK_FLG_DATANR;
1015
1016                 if (archive_mask == 0)
1017                         goto do_ioctl;
1018
1019                 count = hweight32(archive_mask);
1020                 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1021                 OBD_ALLOC(tmp, new_size);
1022                 if (tmp == NULL)
1023                         GOTO(out_lk, rc = -ENOMEM);
1024
1025                 memcpy(tmp, lk, size);
1026                 tmp->lk_data_count = count;
1027                 OBD_FREE(lk, size);
1028                 lk = tmp;
1029                 size = new_size;
1030
1031                 count = 0;
1032                 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1033                         if ((1 << i) & archive_mask) {
1034                                 lk->lk_data[count] = i + 1;
1035                                 count++;
1036                         }
1037                 }
1038                 goto do_ioctl;
1039         }
1040
1041         /* new hsm agent to new mds */
1042         if (lk->lk_data_count > 0) {
1043                 new_size = offsetof(struct lustre_kernelcomm,
1044                                     lk_data[lk->lk_data_count]);
1045                 OBD_ALLOC(tmp, new_size);
1046                 if (tmp == NULL)
1047                         GOTO(out_lk, rc = -ENOMEM);
1048
1049                 OBD_FREE(lk, size);
1050                 lk = tmp;
1051                 size = new_size;
1052
1053                 if (copy_from_user(lk, data, size))
1054                         GOTO(out_lk, rc = -EFAULT);
1055         }
1056
1057         /* new hsm agent to old MDS */
1058         if (!exp_connect_archive_id_array(exp)) {
1059                 __u32 archives = 0;
1060
1061                 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1062                         GOTO(out_lk, rc = -EINVAL);
1063
1064                 for (i = 0; i < lk->lk_data_count; i++) {
1065                         if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1066                                 rc = -EINVAL;
1067                                 CERROR("%s: archive id %d requested but only "
1068                                        "[0 - %zu] supported: rc = %d\n",
1069                                        exp->exp_obd->obd_name, lk->lk_data[i],
1070                                        LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1071                                 GOTO(out_lk, rc);
1072                         }
1073
1074                         if (lk->lk_data[i] == 0) {
1075                                 archives = 0;
1076                                 break;
1077                         }
1078
1079                         archives |= (1 << (lk->lk_data[i] - 1));
1080                 }
1081                 lk->lk_flags &= ~LK_FLG_DATANR;
1082                 lk->lk_data_count = archives;
1083         }
1084 do_ioctl:
1085         rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1086 out_lk:
1087         OBD_FREE(lk, size);
1088         return rc;
1089 }
1090
1091 static int check_owner(int type, int id)
1092 {
1093         switch (type) {
1094         case USRQUOTA:
1095                 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1096                         return -EPERM;
1097                 break;
1098         case GRPQUOTA:
1099                 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1100                         return -EPERM;
1101                 break;
1102         case PRJQUOTA:
1103                 break;
1104         }
1105         return 0;
1106 }
1107
1108 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1109 {
1110         int cmd = qctl->qc_cmd;
1111         int type = qctl->qc_type;
1112         int id = qctl->qc_id;
1113         int valid = qctl->qc_valid;
1114         int rc = 0;
1115         ENTRY;
1116
1117         switch (cmd) {
1118         case Q_SETQUOTA:
1119         case Q_SETINFO:
1120         case LUSTRE_Q_SETDEFAULT:
1121                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1122                         RETURN(-EPERM);
1123                 break;
1124         case Q_GETQUOTA:
1125         case LUSTRE_Q_GETDEFAULT:
1126                 if (check_owner(type, id) &&
1127                     (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1128                         RETURN(-EPERM);
1129                 break;
1130         case Q_GETINFO:
1131                 break;
1132         default:
1133                 CERROR("unsupported quotactl op: %#x\n", cmd);
1134                 RETURN(-ENOTSUPP);
1135         }
1136
1137         if (valid != QC_GENERAL) {
1138                 if (cmd == Q_GETINFO)
1139                         qctl->qc_cmd = Q_GETOINFO;
1140                 else if (cmd == Q_GETQUOTA)
1141                         qctl->qc_cmd = Q_GETOQUOTA;
1142                 else
1143                         RETURN(-EINVAL);
1144
1145                 switch (valid) {
1146                 case QC_MDTIDX:
1147                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1148                                            sizeof(*qctl), qctl, NULL);
1149                         break;
1150                 case QC_OSTIDX:
1151                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1152                                            sizeof(*qctl), qctl, NULL);
1153                         break;
1154                 case QC_UUID:
1155                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1156                                            sizeof(*qctl), qctl, NULL);
1157                         if (rc == -EAGAIN)
1158                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1159                                                    sbi->ll_dt_exp,
1160                                                    sizeof(*qctl), qctl, NULL);
1161                         break;
1162                 default:
1163                         rc = -EINVAL;
1164                         break;
1165                 }
1166
1167                 if (rc)
1168                         RETURN(rc);
1169
1170                 qctl->qc_cmd = cmd;
1171         } else {
1172                 struct obd_quotactl *oqctl;
1173
1174                 OBD_ALLOC_PTR(oqctl);
1175                 if (oqctl == NULL)
1176                         RETURN(-ENOMEM);
1177
1178                 QCTL_COPY(oqctl, qctl);
1179                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1180                 if (rc) {
1181                         OBD_FREE_PTR(oqctl);
1182                         RETURN(rc);
1183                 }
1184                 /* If QIF_SPACE is not set, client should collect the
1185                  * space usage from OSSs by itself */
1186                 if (cmd == Q_GETQUOTA &&
1187                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1188                     !oqctl->qc_dqblk.dqb_curspace) {
1189                         struct obd_quotactl *oqctl_tmp;
1190
1191                         OBD_ALLOC_PTR(oqctl_tmp);
1192                         if (oqctl_tmp == NULL)
1193                                 GOTO(out, rc = -ENOMEM);
1194
1195                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1196                         oqctl_tmp->qc_id = oqctl->qc_id;
1197                         oqctl_tmp->qc_type = oqctl->qc_type;
1198
1199                         /* collect space usage from OSTs */
1200                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1201                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1202                         if (!rc || rc == -EREMOTEIO) {
1203                                 oqctl->qc_dqblk.dqb_curspace =
1204                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1205                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1206                         }
1207
1208                         /* collect space & inode usage from MDTs */
1209                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1210                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1211                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1212                         if (!rc || rc == -EREMOTEIO) {
1213                                 oqctl->qc_dqblk.dqb_curspace +=
1214                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1215                                 oqctl->qc_dqblk.dqb_curinodes =
1216                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1217                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1218                         } else {
1219                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1220                         }
1221
1222                         OBD_FREE_PTR(oqctl_tmp);
1223                 }
1224 out:
1225                 QCTL_COPY(qctl, oqctl);
1226                 OBD_FREE_PTR(oqctl);
1227         }
1228
1229         RETURN(rc);
1230 }
1231
1232 int ll_rmfid(struct file *file, void __user *arg)
1233 {
1234         const struct fid_array __user *ufa = arg;
1235         struct fid_array *lfa = NULL;
1236         size_t size;
1237         unsigned nr;
1238         int i, rc, *rcs = NULL;
1239         ENTRY;
1240
1241         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1242             !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1243                 RETURN(-EPERM);
1244         /* Only need to get the buflen */
1245         if (get_user(nr, &ufa->fa_nr))
1246                 RETURN(-EFAULT);
1247         /* DoS protection */
1248         if (nr > OBD_MAX_FIDS_IN_ARRAY)
1249                 RETURN(-E2BIG);
1250
1251         size = offsetof(struct fid_array, fa_fids[nr]);
1252         OBD_ALLOC(lfa, size);
1253         if (!lfa)
1254                 RETURN(-ENOMEM);
1255         OBD_ALLOC(rcs, sizeof(int) * nr);
1256         if (!rcs)
1257                 GOTO(free_lfa, rc = -ENOMEM);
1258
1259         if (copy_from_user(lfa, arg, size))
1260                 GOTO(free_rcs, rc = -EFAULT);
1261
1262         /* Call mdc_iocontrol */
1263         rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1264         if (!rc) {
1265                 for (i = 0; i < nr; i++)
1266                         if (rcs[i])
1267                                 lfa->fa_fids[i].f_ver = rcs[i];
1268                 if (copy_to_user(arg, lfa, size))
1269                         rc = -EFAULT;
1270         }
1271
1272 free_rcs:
1273         OBD_FREE(rcs, sizeof(int) * nr);
1274 free_lfa:
1275         OBD_FREE(lfa, size);
1276
1277         RETURN(rc);
1278 }
1279
1280 /* This function tries to get a single name component,
1281  * to send to the server. No actual path traversal involved,
1282  * so we limit to NAME_MAX */
1283 static char *ll_getname(const char __user *filename)
1284 {
1285         int ret = 0, len;
1286         char *tmp;
1287
1288         OBD_ALLOC(tmp, NAME_MAX + 1);
1289
1290         if (!tmp)
1291                 return ERR_PTR(-ENOMEM);
1292
1293         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1294         if (len < 0)
1295                 ret = -ENOENT;
1296         else if (len > NAME_MAX)
1297                 ret = -ENAMETOOLONG;
1298
1299         if (ret) {
1300                 OBD_FREE(tmp, NAME_MAX + 1);
1301                 tmp =  ERR_PTR(ret);
1302         }
1303         return tmp;
1304 }
1305
1306 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1307
1308 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1309 {
1310         struct dentry *dentry = file_dentry(file);
1311         struct inode *inode = file_inode(file);
1312         struct ll_sb_info *sbi = ll_i2sbi(inode);
1313         struct obd_ioctl_data *data;
1314         int rc = 0;
1315         ENTRY;
1316
1317         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1318                PFID(ll_inode2fid(inode)), inode, cmd);
1319
1320         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1321         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1322                 return -ENOTTY;
1323
1324         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1325         switch (cmd) {
1326         case FS_IOC_GETFLAGS:
1327         case FS_IOC_SETFLAGS:
1328                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1329         case FSFILT_IOC_GETVERSION:
1330         case FS_IOC_GETVERSION:
1331                 RETURN(put_user(inode->i_generation, (int __user *)arg));
1332         /* We need to special case any other ioctls we want to handle,
1333          * to send them to the MDS/OST as appropriate and to properly
1334          * network encode the arg field. */
1335         case FS_IOC_SETVERSION:
1336                 RETURN(-ENOTSUPP);
1337
1338         case LL_IOC_GET_MDTIDX: {
1339                 int mdtidx;
1340
1341                 mdtidx = ll_get_mdt_idx(inode);
1342                 if (mdtidx < 0)
1343                         RETURN(mdtidx);
1344
1345                 if (put_user((int)mdtidx, (int __user *)arg))
1346                         RETURN(-EFAULT);
1347
1348                 return 0;
1349         }
1350         case IOC_MDC_LOOKUP: {
1351                                      int namelen, len = 0;
1352                 char *buf = NULL;
1353                 char *filename;
1354
1355                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1356                 if (rc != 0)
1357                         RETURN(rc);
1358                 data = (void *)buf;
1359
1360                 filename = data->ioc_inlbuf1;
1361                 namelen = strlen(filename);
1362                 if (namelen < 1) {
1363                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1364                         GOTO(out_free, rc = -EINVAL);
1365                 }
1366
1367                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1368                 if (rc < 0) {
1369                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1370                                sbi->ll_fsname, namelen, filename, rc);
1371                         GOTO(out_free, rc);
1372                 }
1373 out_free:
1374                 OBD_FREE_LARGE(buf, len);
1375                 return rc;
1376         }
1377         case LL_IOC_LMV_SETSTRIPE: {
1378                 struct lmv_user_md  *lum;
1379                 char            *buf = NULL;
1380                 char            *filename;
1381                 int              namelen = 0;
1382                 int              lumlen = 0;
1383                 umode_t          mode;
1384                 int              len;
1385                 int              rc;
1386
1387                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1388                 if (rc)
1389                         RETURN(rc);
1390
1391                 data = (void *)buf;
1392                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1393                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1394                         GOTO(lmv_out_free, rc = -EINVAL);
1395
1396                 filename = data->ioc_inlbuf1;
1397                 namelen = data->ioc_inllen1;
1398
1399                 if (namelen < 1) {
1400                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1401                         GOTO(lmv_out_free, rc = -EINVAL);
1402                 }
1403                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1404                 lumlen = data->ioc_inllen2;
1405
1406                 if (!lmv_magic_supported(lum->lum_magic)) {
1407                         CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1408                                lum->lum_magic, -EINVAL);
1409                         GOTO(lmv_out_free, rc = -EINVAL);
1410                 }
1411
1412                 if ((lum->lum_magic == LMV_USER_MAGIC ||
1413                      lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1414                     lumlen < sizeof(*lum)) {
1415                         CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1416                                filename, lumlen, lum->lum_magic, -EINVAL);
1417                         GOTO(lmv_out_free, rc = -EINVAL);
1418                 }
1419
1420                 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1421                     lumlen < sizeof(struct lmv_foreign_md)) {
1422                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1423                                filename, lum->lum_magic, lumlen, -EFAULT);
1424                         GOTO(lmv_out_free, rc = -EINVAL);
1425                 }
1426
1427 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
1428                 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1429 #else
1430                 mode = data->ioc_type;
1431 #endif
1432                 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1433 lmv_out_free:
1434                 OBD_FREE_LARGE(buf, len);
1435                 RETURN(rc);
1436
1437         }
1438         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1439                 struct lmv_user_md        lum;
1440                 struct lmv_user_md __user *ulump =
1441                                         (struct lmv_user_md __user *)arg;
1442                 int                       rc;
1443
1444                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1445                         RETURN(-EFAULT);
1446
1447                 if (lum.lum_magic != LMV_USER_MAGIC)
1448                         RETURN(-EINVAL);
1449
1450                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1451
1452                 RETURN(rc);
1453         }
1454         case LL_IOC_LOV_SETSTRIPE_NEW:
1455         case LL_IOC_LOV_SETSTRIPE: {
1456                 struct lov_user_md_v3 *lumv3 = NULL;
1457                 struct lov_user_md_v1 lumv1;
1458                 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1459                 struct lov_user_md_v1 __user *lumv1p =
1460                         (struct lov_user_md_v1 __user *)arg;
1461                 struct lov_user_md_v3 __user *lumv3p =
1462                         (struct lov_user_md_v3 __user *)arg;
1463                 int lum_size = 0;
1464
1465                 int set_default = 0;
1466
1467                 CLASSERT(sizeof(struct lov_user_md_v3) >
1468                          sizeof(struct lov_comp_md_v1));
1469                 CLASSERT(sizeof(*lumv3) == sizeof(*lumv3p));
1470                 /* first try with v1 which is smaller than v3 */
1471                 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1472                         RETURN(-EFAULT);
1473
1474                 if (inode->i_sb->s_root == file_dentry(file))
1475                         set_default = 1;
1476
1477                 switch (lumv1.lmm_magic) {
1478                 case LOV_USER_MAGIC_V3:
1479                 case LOV_USER_MAGIC_SPECIFIC:
1480                         lum_size = ll_lov_user_md_size(&lumv1);
1481                         if (lum_size < 0)
1482                                 RETURN(lum_size);
1483                         OBD_ALLOC(lumv3, lum_size);
1484                         if (!lumv3)
1485                                 RETURN(-ENOMEM);
1486                         if (copy_from_user(lumv3, lumv3p, lum_size))
1487                                 GOTO(out, rc = -EFAULT);
1488                         lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1489                         break;
1490                 case LOV_USER_MAGIC_V1:
1491                         break;
1492                 default:
1493                         GOTO(out, rc = -ENOTSUPP);
1494                 }
1495
1496                 /* in v1 and v3 cases lumv1 points to data */
1497                 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1498 out:
1499                 if (lumv3)
1500                         OBD_FREE(lumv3, lum_size);
1501                 RETURN(rc);
1502         }
1503         case LL_IOC_LMV_GETSTRIPE: {
1504                 struct lmv_user_md __user *ulmv =
1505                                         (struct lmv_user_md __user *)arg;
1506                 struct lmv_user_md      lum;
1507                 struct ptlrpc_request   *request = NULL;
1508                 union lmv_mds_md        *lmm = NULL;
1509                 int                     lmmsize;
1510                 u64                     valid = 0;
1511                 struct lmv_user_md      *tmp = NULL;
1512                 int                     mdt_index;
1513                 int                     lum_size;
1514                 int                     stripe_count;
1515                 int                     max_stripe_count;
1516                 int                     i;
1517                 int                     rc;
1518
1519                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1520                         RETURN(-EFAULT);
1521
1522                 max_stripe_count = lum.lum_stripe_count;
1523                 /* lum_magic will indicate which stripe the ioctl will like
1524                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1525                  * is for default LMV stripe */
1526                 if (lum.lum_magic == LMV_MAGIC_V1)
1527                         valid |= OBD_MD_MEA;
1528                 else if (lum.lum_magic == LMV_USER_MAGIC)
1529                         valid |= OBD_MD_DEFAULT_MEA;
1530                 else
1531                         RETURN(-EINVAL);
1532
1533                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1534                                       valid);
1535                 if (rc != 0)
1536                         GOTO(finish_req, rc);
1537
1538                 /* Get default LMV EA */
1539                 if (lum.lum_magic == LMV_USER_MAGIC) {
1540                         if (lmmsize > sizeof(*ulmv))
1541                                 GOTO(finish_req, rc = -EINVAL);
1542
1543                         if (copy_to_user(ulmv, lmm, lmmsize))
1544                                 GOTO(finish_req, rc = -EFAULT);
1545
1546                         GOTO(finish_req, rc);
1547                 }
1548
1549                 /* if foreign LMV case, fake stripes number */
1550                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1551                         struct lmv_foreign_md *lfm;
1552
1553                         lfm = (struct lmv_foreign_md *)lmm;
1554                         if (lfm->lfm_length < XATTR_SIZE_MAX -
1555                             offsetof(typeof(*lfm), lfm_value)) {
1556                                 __u32 size = lfm->lfm_length +
1557                                              offsetof(typeof(*lfm), lfm_value);
1558
1559                                 stripe_count = lmv_foreign_to_md_stripes(size);
1560                         } else {
1561                                 CERROR("invalid %d foreign size returned\n",
1562                                             lfm->lfm_length);
1563                                 return -EINVAL;
1564                         }
1565                 } else {
1566                         stripe_count = lmv_mds_md_stripe_count_get(lmm);
1567                 }
1568                 if (max_stripe_count < stripe_count) {
1569                         lum.lum_stripe_count = stripe_count;
1570                         if (copy_to_user(ulmv, &lum, sizeof(lum)))
1571                                 GOTO(finish_req, rc = -EFAULT);
1572                         GOTO(finish_req, rc = -E2BIG);
1573                 }
1574
1575                 /* enough room on user side and foreign case */
1576                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1577                         struct lmv_foreign_md *lfm;
1578                         __u32 size;
1579
1580                         lfm = (struct lmv_foreign_md *)lmm;
1581                         size = lfm->lfm_length +
1582                                offsetof(struct lmv_foreign_md, lfm_value);
1583                         if (copy_to_user(ulmv, lfm, size))
1584                                 GOTO(finish_req, rc = -EFAULT);
1585                         GOTO(finish_req, rc);
1586                 }
1587
1588                 lum_size = lmv_user_md_size(stripe_count,
1589                                             LMV_USER_MAGIC_SPECIFIC);
1590                 OBD_ALLOC(tmp, lum_size);
1591                 if (tmp == NULL)
1592                         GOTO(finish_req, rc = -ENOMEM);
1593
1594                 mdt_index = ll_get_mdt_idx(inode);
1595                 if (mdt_index < 0)
1596                         GOTO(out_tmp, rc = -ENOMEM);
1597
1598                 tmp->lum_magic = LMV_MAGIC_V1;
1599                 tmp->lum_stripe_count = 0;
1600                 tmp->lum_stripe_offset = mdt_index;
1601                 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1602                 for (i = 0; i < stripe_count; i++) {
1603                         struct lu_fid   fid;
1604
1605                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1606                         if (fid_is_sane(&fid)) {
1607                                 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1608                                 if (mdt_index < 0)
1609                                         GOTO(out_tmp, rc = mdt_index);
1610
1611                                 tmp->lum_objects[i].lum_mds = mdt_index;
1612                                 tmp->lum_objects[i].lum_fid = fid;
1613                         }
1614
1615                         tmp->lum_stripe_count++;
1616                 }
1617
1618                 if (copy_to_user(ulmv, tmp, lum_size))
1619                         GOTO(out_tmp, rc = -EFAULT);
1620 out_tmp:
1621                 OBD_FREE(tmp, lum_size);
1622 finish_req:
1623                 ptlrpc_req_finished(request);
1624                 return rc;
1625         }
1626
1627         case LL_IOC_REMOVE_ENTRY: {
1628                 char            *filename = NULL;
1629                 int              namelen = 0;
1630                 int              rc;
1631
1632                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1633                  * unsupported server, which might crash the server(LU-2730),
1634                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1635                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1636                  * server will support REINT_RMENTRY XXX*/
1637                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1638                         RETURN(-ENOTSUPP);
1639
1640                 filename = ll_getname((const char __user *)arg);
1641                 if (IS_ERR(filename))
1642                         RETURN(PTR_ERR(filename));
1643
1644                 namelen = strlen(filename);
1645                 if (namelen < 1)
1646                         GOTO(out_rmdir, rc = -EINVAL);
1647
1648                 rc = ll_rmdir_entry(inode, filename, namelen);
1649 out_rmdir:
1650                 if (filename)
1651                         ll_putname(filename);
1652                 RETURN(rc);
1653         }
1654         case LL_IOC_RMFID:
1655                 RETURN(ll_rmfid(file, (void __user *)arg));
1656         case LL_IOC_LOV_SWAP_LAYOUTS:
1657                 RETURN(-EPERM);
1658         case IOC_OBD_STATFS:
1659                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1660         case LL_IOC_LOV_GETSTRIPE:
1661         case LL_IOC_LOV_GETSTRIPE_NEW:
1662         case LL_IOC_MDC_GETINFO:
1663         case LL_IOC_MDC_GETINFO_OLD:
1664         case IOC_MDC_GETFILEINFO:
1665         case IOC_MDC_GETFILEINFO_OLD:
1666         case IOC_MDC_GETFILESTRIPE: {
1667                 struct ptlrpc_request *request = NULL;
1668                 struct lov_user_md __user *lump;
1669                 struct lov_mds_md *lmm = NULL;
1670                 struct mdt_body *body;
1671                 char *filename = NULL;
1672                 lstat_t __user *statp = NULL;
1673                 lstatx_t __user *stxp = NULL;
1674                 __u64 __user *flagsp = NULL;
1675                 __u32 __user *lmmsizep = NULL;
1676                 struct lu_fid __user *fidp = NULL;
1677                 int lmmsize;
1678
1679                 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1680                     cmd == IOC_MDC_GETFILEINFO ||
1681                     cmd == IOC_MDC_GETFILESTRIPE) {
1682                         filename = ll_getname((const char __user *)arg);
1683                         if (IS_ERR(filename))
1684                                 RETURN(PTR_ERR(filename));
1685
1686                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1687                                                       &lmmsize, &request);
1688                 } else {
1689                         rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1690                                               &request, 0);
1691                 }
1692
1693                 if (request) {
1694                         body = req_capsule_server_get(&request->rq_pill,
1695                                                       &RMF_MDT_BODY);
1696                         LASSERT(body != NULL);
1697                 } else {
1698                         GOTO(out_req, rc);
1699                 }
1700
1701                 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1702                                        cmd == LL_IOC_MDC_GETINFO ||
1703                                        cmd == IOC_MDC_GETFILEINFO_OLD ||
1704                                        cmd == LL_IOC_MDC_GETINFO_OLD)) {
1705                         lmmsize = 0;
1706                         rc = 0;
1707                 }
1708
1709                 if (rc < 0)
1710                         GOTO(out_req, rc);
1711
1712                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1713                     cmd == LL_IOC_LOV_GETSTRIPE ||
1714                     cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1715                         lump = (struct lov_user_md __user *)arg;
1716                 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1717                            cmd == LL_IOC_MDC_GETINFO_OLD){
1718                         struct lov_user_mds_data_v1 __user *lmdp;
1719
1720                         lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1721                         statp = &lmdp->lmd_st;
1722                         lump = &lmdp->lmd_lmm;
1723                 } else {
1724                         struct lov_user_mds_data __user *lmdp;
1725
1726                         lmdp = (struct lov_user_mds_data __user *)arg;
1727                         fidp = &lmdp->lmd_fid;
1728                         stxp = &lmdp->lmd_stx;
1729                         flagsp = &lmdp->lmd_flags;
1730                         lmmsizep = &lmdp->lmd_lmmsize;
1731                         lump = &lmdp->lmd_lmm;
1732                 }
1733
1734                 if (lmmsize == 0) {
1735                         /* If the file has no striping then zero out *lump so
1736                          * that the caller isn't confused by garbage. */
1737                         if (clear_user(lump, sizeof(*lump)))
1738                                 GOTO(out_req, rc = -EFAULT);
1739                 } else if (copy_to_user(lump, lmm, lmmsize)) {
1740                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1741                                 GOTO(out_req, rc = -EFAULT);
1742                         rc = -EOVERFLOW;
1743                 }
1744
1745                 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1746                     cmd == LL_IOC_MDC_GETINFO_OLD) {
1747                         lstat_t st = { 0 };
1748
1749                         st.st_dev       = inode->i_sb->s_dev;
1750                         st.st_mode      = body->mbo_mode;
1751                         st.st_nlink     = body->mbo_nlink;
1752                         st.st_uid       = body->mbo_uid;
1753                         st.st_gid       = body->mbo_gid;
1754                         st.st_rdev      = body->mbo_rdev;
1755                         st.st_size      = body->mbo_size;
1756                         st.st_blksize   = PAGE_SIZE;
1757                         st.st_blocks    = body->mbo_blocks;
1758                         st.st_atime     = body->mbo_atime;
1759                         st.st_mtime     = body->mbo_mtime;
1760                         st.st_ctime     = body->mbo_ctime;
1761                         st.st_ino       = cl_fid_build_ino(&body->mbo_fid1,
1762                                                 sbi->ll_flags &
1763                                                 LL_SBI_32BIT_API);
1764
1765                         if (copy_to_user(statp, &st, sizeof(st)))
1766                                 GOTO(out_req, rc = -EFAULT);
1767                 } else if (cmd == IOC_MDC_GETFILEINFO ||
1768                            cmd == LL_IOC_MDC_GETINFO) {
1769                         lstatx_t stx = { 0 };
1770                         __u64 valid = body->mbo_valid;
1771
1772                         stx.stx_blksize = PAGE_SIZE;
1773                         stx.stx_nlink = body->mbo_nlink;
1774                         stx.stx_uid = body->mbo_uid;
1775                         stx.stx_gid = body->mbo_gid;
1776                         stx.stx_mode = body->mbo_mode;
1777                         stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1778                                                        sbi->ll_flags &
1779                                                        LL_SBI_32BIT_API);
1780                         stx.stx_size = body->mbo_size;
1781                         stx.stx_blocks = body->mbo_blocks;
1782                         stx.stx_atime.tv_sec = body->mbo_atime;
1783                         stx.stx_ctime.tv_sec = body->mbo_ctime;
1784                         stx.stx_mtime.tv_sec = body->mbo_mtime;
1785                         stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1786                         stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1787                         stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1788                         stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1789                         stx.stx_mask |= STATX_BASIC_STATS;
1790
1791                         /*
1792                          * For a striped directory, the size and blocks returned
1793                          * from MDT is not correct.
1794                          * The size and blocks are aggregated by client across
1795                          * all stripes.
1796                          * Thus for a striped directory, do not return the valid
1797                          * FLSIZE and FLBLOCKS flags to the caller.
1798                          * However, this whould be better decided by the MDS
1799                          * instead of the client.
1800                          */
1801                         if (cmd == LL_IOC_MDC_GETINFO &&
1802                             ll_i2info(inode)->lli_lsm_md != NULL)
1803                                 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1804
1805                         if (flagsp && copy_to_user(flagsp, &valid,
1806                                                    sizeof(*flagsp)))
1807                                 GOTO(out_req, rc = -EFAULT);
1808
1809                         if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1810                                                  sizeof(*fidp)))
1811                                 GOTO(out_req, rc = -EFAULT);
1812
1813                         if (!(valid & OBD_MD_FLSIZE))
1814                                 stx.stx_mask &= ~STATX_SIZE;
1815                         if (!(valid & OBD_MD_FLBLOCKS))
1816                                 stx.stx_mask &= ~STATX_BLOCKS;
1817
1818                         if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1819                                 GOTO(out_req, rc = -EFAULT);
1820
1821                         if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1822                                                      sizeof(*lmmsizep)))
1823                                 GOTO(out_req, rc = -EFAULT);
1824                 }
1825
1826                 EXIT;
1827 out_req:
1828                 ptlrpc_req_finished(request);
1829                 if (filename)
1830                         ll_putname(filename);
1831                 return rc;
1832         }
1833         case OBD_IOC_QUOTACTL: {
1834                 struct if_quotactl *qctl;
1835
1836                 OBD_ALLOC_PTR(qctl);
1837                 if (!qctl)
1838                         RETURN(-ENOMEM);
1839
1840                 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1841                         GOTO(out_quotactl, rc = -EFAULT);
1842
1843                 rc = quotactl_ioctl(sbi, qctl);
1844
1845                 if (rc == 0 &&
1846                     copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1847                         rc = -EFAULT;
1848
1849         out_quotactl:
1850                 OBD_FREE_PTR(qctl);
1851                 RETURN(rc);
1852         }
1853         case OBD_IOC_GETDTNAME:
1854         case OBD_IOC_GETMDNAME:
1855                 RETURN(ll_get_obd_name(inode, cmd, arg));
1856         case LL_IOC_FLUSHCTX:
1857                 RETURN(ll_flush_ctx(inode));
1858         case LL_IOC_GETOBDCOUNT: {
1859                 u32 count, vallen;
1860                 struct obd_export *exp;
1861
1862                 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1863                         RETURN(-EFAULT);
1864
1865                 /* get ost count when count is zero, get mdt count otherwise */
1866                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1867                 vallen = sizeof(count);
1868                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1869                                   KEY_TGT_COUNT, &vallen, &count);
1870                 if (rc) {
1871                         CERROR("get target count failed: %d\n", rc);
1872                         RETURN(rc);
1873                 }
1874
1875                 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1876                         RETURN(-EFAULT);
1877
1878                 RETURN(0);
1879         }
1880         case LL_IOC_PATH2FID:
1881                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1882                                      sizeof(struct lu_fid)))
1883                         RETURN(-EFAULT);
1884                 RETURN(0);
1885         case LL_IOC_GET_CONNECT_FLAGS: {
1886                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1887                                      (void __user *)arg));
1888         }
1889         case OBD_IOC_FID2PATH:
1890                 RETURN(ll_fid2path(inode, (void __user *)arg));
1891         case LL_IOC_GETPARENT:
1892                 RETURN(ll_getparent(file, (void __user *)arg));
1893         case LL_IOC_FID2MDTIDX: {
1894                 struct obd_export *exp = ll_i2mdexp(inode);
1895                 struct lu_fid     fid;
1896                 __u32             index;
1897
1898                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1899                                    sizeof(fid)))
1900                         RETURN(-EFAULT);
1901
1902                 /* Call mdc_iocontrol */
1903                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1904                                    (__u32 __user *)&index);
1905                 if (rc != 0)
1906                         RETURN(rc);
1907
1908                 RETURN(index);
1909         }
1910         case LL_IOC_HSM_REQUEST: {
1911                 struct hsm_user_request *hur;
1912                 ssize_t                  totalsize;
1913
1914                 OBD_ALLOC_PTR(hur);
1915                 if (hur == NULL)
1916                         RETURN(-ENOMEM);
1917
1918                 /* We don't know the true size yet; copy the fixed-size part */
1919                 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1920                         OBD_FREE_PTR(hur);
1921                         RETURN(-EFAULT);
1922                 }
1923
1924                 /* Compute the whole struct size */
1925                 totalsize = hur_len(hur);
1926                 OBD_FREE_PTR(hur);
1927                 if (totalsize < 0)
1928                         RETURN(-E2BIG);
1929
1930                 /* Final size will be more than double totalsize */
1931                 if (totalsize >= MDS_MAXREQSIZE / 3)
1932                         RETURN(-E2BIG);
1933
1934                 OBD_ALLOC_LARGE(hur, totalsize);
1935                 if (hur == NULL)
1936                         RETURN(-ENOMEM);
1937
1938                 /* Copy the whole struct */
1939                 if (copy_from_user(hur, (void __user *)arg, totalsize))
1940                         GOTO(out_hur, rc = -EFAULT);
1941
1942                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1943                         const struct lu_fid *fid;
1944                         struct inode *f;
1945                         int i;
1946
1947                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1948                                 fid = &hur->hur_user_item[i].hui_fid;
1949                                 f = search_inode_for_lustre(inode->i_sb, fid);
1950                                 if (IS_ERR(f)) {
1951                                         rc = PTR_ERR(f);
1952                                         break;
1953                                 }
1954
1955                                 rc = ll_hsm_release(f);
1956                                 iput(f);
1957                                 if (rc != 0)
1958                                         break;
1959                         }
1960                 } else {
1961                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1962                                            hur, NULL);
1963                 }
1964
1965 out_hur:
1966                 OBD_FREE_LARGE(hur, totalsize);
1967
1968                 RETURN(rc);
1969         }
1970         case LL_IOC_HSM_PROGRESS: {
1971                 struct hsm_progress_kernel      hpk;
1972                 struct hsm_progress             hp;
1973
1974                 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1975                         RETURN(-EFAULT);
1976
1977                 hpk.hpk_fid = hp.hp_fid;
1978                 hpk.hpk_cookie = hp.hp_cookie;
1979                 hpk.hpk_extent = hp.hp_extent;
1980                 hpk.hpk_flags = hp.hp_flags;
1981                 hpk.hpk_errval = hp.hp_errval;
1982                 hpk.hpk_data_version = 0;
1983
1984                 /* File may not exist in Lustre; all progress
1985                  * reported to Lustre root */
1986                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1987                                    NULL);
1988                 RETURN(rc);
1989         }
1990         case LL_IOC_HSM_CT_START:
1991                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1992                         RETURN(-EPERM);
1993
1994                 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
1995                                        (struct lustre_kernelcomm __user *)arg);
1996                 RETURN(rc);
1997
1998         case LL_IOC_HSM_COPY_START: {
1999                 struct hsm_copy *copy;
2000                 int              rc;
2001
2002                 OBD_ALLOC_PTR(copy);
2003                 if (copy == NULL)
2004                         RETURN(-ENOMEM);
2005                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2006                         OBD_FREE_PTR(copy);
2007                         RETURN(-EFAULT);
2008                 }
2009
2010                 rc = ll_ioc_copy_start(inode->i_sb, copy);
2011                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2012                         rc = -EFAULT;
2013
2014                 OBD_FREE_PTR(copy);
2015                 RETURN(rc);
2016         }
2017         case LL_IOC_HSM_COPY_END: {
2018                 struct hsm_copy *copy;
2019                 int              rc;
2020
2021                 OBD_ALLOC_PTR(copy);
2022                 if (copy == NULL)
2023                         RETURN(-ENOMEM);
2024                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2025                         OBD_FREE_PTR(copy);
2026                         RETURN(-EFAULT);
2027                 }
2028
2029                 rc = ll_ioc_copy_end(inode->i_sb, copy);
2030                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2031                         rc = -EFAULT;
2032
2033                 OBD_FREE_PTR(copy);
2034                 RETURN(rc);
2035         }
2036         case LL_IOC_MIGRATE: {
2037                 struct lmv_user_md *lum;
2038                 char *buf = NULL;
2039                 int len;
2040                 char *filename;
2041                 int namelen = 0;
2042                 int rc;
2043
2044                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2045                 if (rc)
2046                         RETURN(rc);
2047
2048                 data = (struct obd_ioctl_data *)buf;
2049                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2050                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2051                         GOTO(migrate_free, rc = -EINVAL);
2052
2053                 filename = data->ioc_inlbuf1;
2054                 namelen = data->ioc_inllen1;
2055
2056                 if (namelen < 1 || namelen != strlen(filename) + 1) {
2057                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2058                         GOTO(migrate_free, rc = -EINVAL);
2059                 }
2060
2061                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2062                 if (lum->lum_magic != LMV_USER_MAGIC &&
2063                     lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2064                         rc = -EINVAL;
2065                         CERROR("%s: wrong lum magic %x: rc = %d\n",
2066                                filename, lum->lum_magic, rc);
2067                         GOTO(migrate_free, rc);
2068                 }
2069
2070                 rc = ll_migrate(inode, file, lum, filename);
2071 migrate_free:
2072                 OBD_FREE_LARGE(buf, len);
2073
2074                 RETURN(rc);
2075         }
2076         case LL_IOC_FSGETXATTR:
2077                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2078         case LL_IOC_FSSETXATTR:
2079                 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2080         case LL_IOC_PCC_DETACH_BY_FID: {
2081                 struct lu_pcc_detach_fid *detach;
2082                 struct lu_fid *fid;
2083                 struct inode *inode2;
2084                 unsigned long ino;
2085
2086                 OBD_ALLOC_PTR(detach);
2087                 if (detach == NULL)
2088                         RETURN(-ENOMEM);
2089
2090                 if (copy_from_user(detach,
2091                                    (const struct lu_pcc_detach_fid __user *)arg,
2092                                    sizeof(*detach)))
2093                         GOTO(out_detach, rc = -EFAULT);
2094
2095                 fid = &detach->pccd_fid;
2096                 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2097                 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2098                 if (inode2 == NULL)
2099                         /* Target inode is not in inode cache, and PCC file
2100                          * has aleady released, return immdiately.
2101                          */
2102                         GOTO(out_detach, rc = 0);
2103
2104                 if (!S_ISREG(inode2->i_mode))
2105                         GOTO(out_iput, rc = -EINVAL);
2106
2107                 if (!inode_owner_or_capable(inode2))
2108                         GOTO(out_iput, rc = -EPERM);
2109
2110                 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2111 out_iput:
2112                 iput(inode2);
2113 out_detach:
2114                 OBD_FREE_PTR(detach);
2115                 RETURN(rc);
2116         }
2117         default:
2118                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2119                                      (void __user *)arg));
2120         }
2121 }
2122
2123 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2124 {
2125         struct inode *inode = file->f_mapping->host;
2126         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2127         struct ll_sb_info *sbi = ll_i2sbi(inode);
2128         int api32 = ll_need_32bit_api(sbi);
2129         loff_t ret = -EINVAL;
2130         ENTRY;
2131
2132         inode_lock(inode);
2133         switch (origin) {
2134                 case SEEK_SET:
2135                         break;
2136                 case SEEK_CUR:
2137                         offset += file->f_pos;
2138                         break;
2139                 case SEEK_END:
2140                         if (offset > 0)
2141                                 GOTO(out, ret);
2142                         if (api32)
2143                                 offset += LL_DIR_END_OFF_32BIT;
2144                         else
2145                                 offset += LL_DIR_END_OFF;
2146                         break;
2147                 default:
2148                         GOTO(out, ret);
2149         }
2150
2151         if (offset >= 0 &&
2152             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2153              (!api32 && offset <= LL_DIR_END_OFF))) {
2154                 if (offset != file->f_pos) {
2155                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2156                             (!api32 && offset == LL_DIR_END_OFF))
2157                                 fd->lfd_pos = MDS_DIR_END_OFF;
2158                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2159                                 fd->lfd_pos = offset << 32;
2160                         else
2161                                 fd->lfd_pos = offset;
2162                         file->f_pos = offset;
2163                         file->f_version = 0;
2164                 }
2165                 ret = offset;
2166         }
2167         GOTO(out, ret);
2168
2169 out:
2170         inode_unlock(inode);
2171         return ret;
2172 }
2173
2174 static int ll_dir_open(struct inode *inode, struct file *file)
2175 {
2176         ENTRY;
2177         RETURN(ll_file_open(inode, file));
2178 }
2179
2180 static int ll_dir_release(struct inode *inode, struct file *file)
2181 {
2182         ENTRY;
2183         RETURN(ll_file_release(inode, file));
2184 }
2185
2186 const struct file_operations ll_dir_operations = {
2187         .llseek         = ll_dir_seek,
2188         .open           = ll_dir_open,
2189         .release        = ll_dir_release,
2190         .read           = generic_read_dir,
2191 #ifdef HAVE_DIR_CONTEXT
2192         .iterate_shared = ll_iterate,
2193 #else
2194         .readdir        = ll_readdir,
2195 #endif
2196         .unlocked_ioctl = ll_dir_ioctl,
2197         .fsync          = ll_fsync,
2198 };