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