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