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