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