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