Whamcloud - gitweb
LU-5560 tests: add sanity-selinux.sh
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/dir.c
37  *
38  * Directory code for lustre client.
39  */
40
41 #include <linux/fs.h>
42 #include <linux/pagemap.h>
43 #include <linux/mm.h>
44 #include <linux/version.h>
45 #include <linux/user_namespace.h>
46 #ifdef HAVE_UIDGID_HEADER
47 # include <linux/uidgid.h>
48 #endif
49 #include <asm/uaccess.h>
50 #include <linux/buffer_head.h>   // for wait_on_buffer
51 #include <linux/pagevec.h>
52
53 #define DEBUG_SUBSYSTEM S_LLITE
54
55 #include <lustre/lustre_idl.h>
56
57 #include <obd_support.h>
58 #include <obd_class.h>
59 #include <lustre_ioctl.h>
60 #include <lustre_lib.h>
61 #include <lustre_dlm.h>
62 #include <lustre_fid.h>
63 #include <lustre_kernelcomm.h>
64 #include <lustre_swab.h>
65
66 #include "llite_internal.h"
67
68 /*
69  * (new) readdir implementation overview.
70  *
71  * Original lustre readdir implementation cached exact copy of raw directory
72  * pages on the client. These pages were indexed in client page cache by
73  * logical offset in the directory file. This design, while very simple and
74  * intuitive had some inherent problems:
75  *
76  *     . it implies that byte offset to the directory entry serves as a
77  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
78  *     ext3/htree directory entries may move due to splits, and more
79  *     importantly,
80  *
81  *     . it is incompatible with the design of split directories for cmd3,
82  *     that assumes that names are distributed across nodes based on their
83  *     hash, and so readdir should be done in hash order.
84  *
85  * New readdir implementation does readdir in hash order, and uses hash of a
86  * file name as a telldir/seekdir cookie. This led to number of complications:
87  *
88  *     . hash is not unique, so it cannot be used to index cached directory
89  *     pages on the client (note, that it requires a whole pageful of hash
90  *     collided entries to cause two pages to have identical hashes);
91  *
92  *     . hash is not unique, so it cannot, strictly speaking, be used as an
93  *     entry cookie. ext3/htree has the same problem and lustre implementation
94  *     mimics their solution: seekdir(hash) positions directory at the first
95  *     entry with the given hash.
96  *
97  * Client side.
98  *
99  * 0. caching
100  *
101  * Client caches directory pages using hash of the first entry as an index. As
102  * noted above hash is not unique, so this solution doesn't work as is:
103  * special processing is needed for "page hash chains" (i.e., sequences of
104  * pages filled with entries all having the same hash value).
105  *
106  * First, such chains have to be detected. To this end, server returns to the
107  * client the hash of the first entry on the page next to one returned. When
108  * client detects that this hash is the same as hash of the first entry on the
109  * returned page, page hash collision has to be handled. Pages in the
110  * hash chain, except first one, are termed "overflow pages".
111  *
112  * Solution to index uniqueness problem is to not cache overflow
113  * pages. Instead, when page hash collision is detected, all overflow pages
114  * from emerging chain are immediately requested from the server and placed in
115  * a special data structure (struct ll_dir_chain). This data structure is used
116  * by ll_readdir() to process entries from overflow pages. When readdir
117  * invocation finishes, overflow pages are discarded. If page hash collision
118  * chain weren't completely processed, next call to readdir will again detect
119  * page hash collision, again read overflow pages in, process next portion of
120  * entries and again discard the pages. This is not as wasteful as it looks,
121  * because, given reasonable hash, page hash collisions are extremely rare.
122  *
123  * 1. directory positioning
124  *
125  * When seekdir(hash) is called, original
126  *
127  *
128  *
129  *
130  *
131  *
132  *
133  *
134  * Server.
135  *
136  * identification of and access to overflow pages
137  *
138  * page format
139  *
140  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
141  * a header lu_dirpage which describes the start/end hash, and whether this
142  * page is empty (contains no dir entry) or hash collide with next page.
143  * After client receives reply, several pages will be integrated into dir page
144  * in PAGE_CACHE_SIZE (if PAGE_CACHE_SIZE greater than LU_PAGE_SIZE), and the
145  * lu_dirpage for this integrated page will be adjusted. See
146  * mdc_adjust_dirpages().
147  *
148  */
149 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
150                              __u64 offset, struct ll_dir_chain *chain)
151 {
152         struct md_callback      cb_op;
153         struct page             *page;
154         int                     rc;
155
156         cb_op.md_blocking_ast = ll_md_blocking_ast;
157         rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
158         if (rc != 0)
159                 return ERR_PTR(rc);
160
161         return page;
162 }
163
164 void ll_release_page(struct inode *inode, struct page *page,
165                      bool remove)
166 {
167         kunmap(page);
168
169         /* Always remove the page for striped dir, because the page is
170          * built from temporarily in LMV layer */
171         if (inode != NULL && S_ISDIR(inode->i_mode) &&
172             ll_i2info(inode)->lli_lsm_md != NULL) {
173                 __free_page(page);
174                 return;
175         }
176
177         if (remove) {
178                 lock_page(page);
179                 if (likely(page->mapping != NULL))
180                         truncate_complete_page(page->mapping, page);
181                 unlock_page(page);
182         }
183         page_cache_release(page);
184 }
185
186 /**
187  * return IF_* type for given lu_dirent entry.
188  * IF_* flag shld be converted to particular OS file type in
189  * platform llite module.
190  */
191 static u16 ll_dirent_type_get(struct lu_dirent *ent)
192 {
193         u16 type = 0;
194         struct luda_type *lt;
195         int len = 0;
196
197         if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
198                 const unsigned align = sizeof(struct luda_type) - 1;
199
200                 len = le16_to_cpu(ent->lde_namelen);
201                 len = (len + align) & ~align;
202                 lt = (void *)ent->lde_name + len;
203                 type = IFTODT(le16_to_cpu(lt->lt_type));
204         }
205
206         return type;
207 }
208
209 #ifdef HAVE_DIR_CONTEXT
210 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
211                 struct dir_context *ctx)
212 {
213 #else
214 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
215                 void *cookie, filldir_t filldir)
216 {
217 #endif
218         struct ll_sb_info    *sbi        = ll_i2sbi(inode);
219         __u64                 pos        = *ppos;
220         bool                  is_api32 = ll_need_32bit_api(sbi);
221         bool                  is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
222         struct page          *page;
223         struct ll_dir_chain   chain;
224         bool                  done = false;
225         int                   rc = 0;
226         ENTRY;
227
228         ll_dir_chain_init(&chain);
229
230         page = ll_get_dir_page(inode, op_data, pos, &chain);
231
232         while (rc == 0 && !done) {
233                 struct lu_dirpage *dp;
234                 struct lu_dirent  *ent;
235                 __u64 hash;
236                 __u64 next;
237
238                 if (IS_ERR(page)) {
239                         rc = PTR_ERR(page);
240                         break;
241                 }
242
243                 hash = MDS_DIR_END_OFF;
244                 dp = page_address(page);
245                 for (ent = lu_dirent_start(dp); ent != NULL && !done;
246                      ent = lu_dirent_next(ent)) {
247                         __u16          type;
248                         int            namelen;
249                         struct lu_fid  fid;
250                         __u64          lhash;
251                         __u64          ino;
252
253                         hash = le64_to_cpu(ent->lde_hash);
254                         if (hash < pos)
255                                 /*
256                                  * Skip until we find target hash
257                                  * value.
258                                  */
259                                 continue;
260
261                         namelen = le16_to_cpu(ent->lde_namelen);
262                         if (namelen == 0)
263                                 /*
264                                  * Skip dummy record.
265                                  */
266                                 continue;
267
268                         if (is_api32 && is_hash64)
269                                 lhash = hash >> 32;
270                         else
271                                 lhash = hash;
272                         fid_le_to_cpu(&fid, &ent->lde_fid);
273                         ino = cl_fid_build_ino(&fid, is_api32);
274                         type = ll_dirent_type_get(ent);
275                         /* For 'll_nfs_get_name_filldir()', it will try
276                          * to access the 'ent' through its 'lde_name',
277                          * so the parameter 'name' for 'filldir()' must
278                          * be part of the 'ent'. */
279 #ifdef HAVE_DIR_CONTEXT
280                         ctx->pos = lhash;
281                         done = !dir_emit(ctx, ent->lde_name, namelen, ino,
282                                          type);
283 #else
284                         done = filldir(cookie, ent->lde_name, namelen, lhash,
285                                        ino, type);
286 #endif
287                 }
288
289                 if (done) {
290                         pos = hash;
291                         ll_release_page(inode, page, false);
292                         break;
293                 }
294
295                 next = le64_to_cpu(dp->ldp_hash_end);
296                 pos = next;
297                 if (pos == MDS_DIR_END_OFF) {
298                         /*
299                          * End of directory reached.
300                          */
301                         done = 1;
302                         ll_release_page(inode, page, false);
303                 } else {
304                         /*
305                          * Normal case: continue to the next
306                          * page.
307                          */
308                         ll_release_page(inode, page,
309                                         le32_to_cpu(dp->ldp_flags) &
310                                         LDF_COLLIDE);
311                         next = pos;
312                         page = ll_get_dir_page(inode, op_data, pos,
313                                                &chain);
314                 }
315         }
316 #ifdef HAVE_DIR_CONTEXT
317         ctx->pos = pos;
318 #else
319         *ppos = pos;
320 #endif
321         ll_dir_chain_fini(&chain);
322         RETURN(rc);
323 }
324
325 #ifdef HAVE_DIR_CONTEXT
326 static int ll_iterate(struct file *filp, struct dir_context *ctx)
327 #else
328 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
329 #endif
330 {
331         struct inode            *inode  = file_inode(filp);
332         struct ll_file_data     *lfd    = LUSTRE_FPRIVATE(filp);
333         struct ll_sb_info       *sbi    = ll_i2sbi(inode);
334         int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
335         int                     api32   = ll_need_32bit_api(sbi);
336         struct md_op_data       *op_data;
337         __u64                   pos;
338         int                     rc;
339         ENTRY;
340
341         if (lfd != NULL)
342                 pos = lfd->lfd_pos;
343         else
344                 pos = 0;
345
346         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) pos/size"
347                "%lu/%llu 32bit_api %d\n", PFID(ll_inode2fid(inode)),
348                inode, (unsigned long)pos, i_size_read(inode), api32);
349
350         if (pos == MDS_DIR_END_OFF)
351                 /*
352                  * end-of-file.
353                  */
354                 GOTO(out, rc = 0);
355
356         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
357                                      LUSTRE_OPC_ANY, inode);
358         if (IS_ERR(op_data))
359                 GOTO(out, rc = PTR_ERR(op_data));
360
361         if (unlikely(op_data->op_mea1 != NULL)) {
362                 /* This is only needed for striped dir to fill ..,
363                  * see lmv_read_entry */
364                 if (file_dentry(filp)->d_parent != NULL &&
365                     file_dentry(filp)->d_parent->d_inode != NULL) {
366                         __u64 ibits = MDS_INODELOCK_UPDATE;
367                         struct inode *parent =
368                                 file_dentry(filp)->d_parent->d_inode;
369
370                         if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
371                                 op_data->op_fid3 = *ll_inode2fid(parent);
372                 }
373
374                 /* If it can not find in cache, do lookup .. on the master
375                  * object */
376                 if (fid_is_zero(&op_data->op_fid3)) {
377                         rc = ll_dir_get_parent_fid(inode, &op_data->op_fid3);
378                         if (rc != 0) {
379                                 ll_finish_md_op_data(op_data);
380                                 RETURN(rc);
381                         }
382                 }
383         }
384         op_data->op_max_pages = sbi->ll_md_brw_pages;
385 #ifdef HAVE_DIR_CONTEXT
386         ctx->pos = pos;
387         rc = ll_dir_read(inode, &pos, op_data, ctx);
388         pos = ctx->pos;
389 #else
390         rc = ll_dir_read(inode, &pos, op_data, cookie, filldir);
391 #endif
392         if (lfd != NULL)
393                 lfd->lfd_pos = pos;
394
395         if (pos == MDS_DIR_END_OFF) {
396                 if (api32)
397                         pos = LL_DIR_END_OFF_32BIT;
398                 else
399                         pos = LL_DIR_END_OFF;
400         } else {
401                 if (api32 && hash64)
402                         pos = pos >> 32;
403         }
404 #ifdef HAVE_DIR_CONTEXT
405         ctx->pos = pos;
406 #else
407         filp->f_pos = pos;
408 #endif
409         ll_finish_md_op_data(op_data);
410         filp->f_version = inode->i_version;
411
412 out:
413         if (!rc)
414                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
415
416         RETURN(rc);
417 }
418
419 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
420 {
421         struct mgs_send_param *msp;
422         int rc = 0;
423
424         OBD_ALLOC_PTR(msp);
425         if (!msp)
426                 return -ENOMEM;
427
428         strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
429         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
430                                 sizeof(struct mgs_send_param), msp, NULL);
431         if (rc)
432                 CERROR("Failed to set parameter: %d\n", rc);
433         OBD_FREE_PTR(msp);
434
435         return rc;
436 }
437
438 /**
439  * Create striped directory with specified stripe(@lump)
440  *
441  * param[in]parent      the parent of the directory.
442  * param[in]lump        the specified stripes.
443  * param[in]dirname     the name of the directory.
444  * param[in]mode        the specified mode of the directory.
445  *
446  * retval               =0 if striped directory is being created successfully.
447  *                      <0 if the creation is failed.
448  */
449 static int ll_dir_setdirstripe(struct inode *parent, struct lmv_user_md *lump,
450                                const char *dirname, umode_t mode)
451 {
452         struct ptlrpc_request *request = NULL;
453         struct md_op_data *op_data;
454         struct ll_sb_info *sbi = ll_i2sbi(parent);
455         struct inode *inode = NULL;
456         struct dentry dentry;
457         int err;
458         ENTRY;
459
460         if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
461                 RETURN(-EINVAL);
462
463         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) name %s"
464                "stripe_offset %d, stripe_count: %u\n",
465                PFID(ll_inode2fid(parent)), parent, dirname,
466                (int)lump->lum_stripe_offset, lump->lum_stripe_count);
467
468         if (lump->lum_stripe_count > 1 &&
469             !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
470                 RETURN(-EINVAL);
471
472         if (IS_DEADDIR(parent) &&
473             !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
474                 RETURN(-ENOENT);
475
476         if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
477                 lustre_swab_lmv_user_md(lump);
478
479         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
480                 mode &= ~current_umask();
481         mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
482         op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
483                                      strlen(dirname), mode, LUSTRE_OPC_MKDIR,
484                                      lump);
485         if (IS_ERR(op_data))
486                 GOTO(err_exit, err = PTR_ERR(op_data));
487
488         op_data->op_cli_flags |= CLI_SET_MEA;
489         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
490                         from_kuid(&init_user_ns, current_fsuid()),
491                         from_kgid(&init_user_ns, current_fsgid()),
492                         cfs_curproc_cap_pack(), 0, &request);
493         ll_finish_md_op_data(op_data);
494         if (err)
495                 GOTO(err_exit, err);
496
497         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
498
499         err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
500         if (err)
501                 GOTO(err_exit, err);
502
503         memset(&dentry, 0, sizeof(dentry));
504         dentry.d_inode = inode;
505
506         err = ll_init_security(&dentry, inode, parent);
507         iput(inode);
508         if (err)
509                 GOTO(err_exit, err);
510
511 err_exit:
512         ptlrpc_req_finished(request);
513         return err;
514 }
515
516 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
517                      int set_default)
518 {
519         struct ll_sb_info *sbi = ll_i2sbi(inode);
520         struct md_op_data *op_data;
521         struct ptlrpc_request *req = NULL;
522         int rc = 0;
523         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
524         struct obd_device *mgc = lsi->lsi_mgc;
525         int lum_size;
526         ENTRY;
527
528         if (lump != NULL) {
529                 /*
530                  * This is coming from userspace, so should be in
531                  * local endian.  But the MDS would like it in little
532                  * endian, so we swab it before we send it.
533                  */
534                 switch (lump->lmm_magic) {
535                 case LOV_USER_MAGIC_V1: {
536                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
537                                 lustre_swab_lov_user_md_v1(lump);
538                         lum_size = sizeof(struct lov_user_md_v1);
539                         break;
540                 }
541                 case LOV_USER_MAGIC_V3: {
542                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
543                                 lustre_swab_lov_user_md_v3(
544                                         (struct lov_user_md_v3 *)lump);
545                         lum_size = sizeof(struct lov_user_md_v3);
546                         break;
547                 }
548                 case LMV_USER_MAGIC: {
549                         if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
550                                 lustre_swab_lmv_user_md(
551                                         (struct lmv_user_md *)lump);
552                         lum_size = sizeof(struct lmv_user_md);
553                         break;
554                 }
555                 default: {
556                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
557                                         " %#08x != %#08x nor %#08x\n",
558                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
559                                         LOV_USER_MAGIC_V3);
560                         RETURN(-EINVAL);
561                 }
562                 }
563         } else {
564                 lum_size = sizeof(struct lov_user_md_v1);
565         }
566
567         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
568                                      LUSTRE_OPC_ANY, NULL);
569         if (IS_ERR(op_data))
570                 RETURN(PTR_ERR(op_data));
571
572         /* swabbing is done in lov_setstripe() on server side */
573         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
574         ll_finish_md_op_data(op_data);
575         ptlrpc_req_finished(req);
576         if (rc) {
577                 if (rc != -EPERM && rc != -EACCES)
578                         CERROR("mdc_setattr fails: rc = %d\n", rc);
579         }
580
581         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
582          LOV_USER_MAGIC_V3 have the same initial fields so we do not
583          need the make the distiction between the 2 versions */
584         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
585                 char *param = NULL;
586                 char *buf;
587
588                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
589                 if (param == NULL)
590                         GOTO(end, rc = -ENOMEM);
591
592                 buf = param;
593                 /* Get fsname and assume devname to be -MDT0000. */
594                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
595                 strcat(buf, "-MDT0000.lov");
596                 buf += strlen(buf);
597
598                 /* Set root stripesize */
599                 sprintf(buf, ".stripesize=%u",
600                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
601                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
602                 if (rc)
603                         GOTO(end, rc);
604
605                 /* Set root stripecount */
606                 sprintf(buf, ".stripecount=%hd",
607                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
608                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
609                 if (rc)
610                         GOTO(end, rc);
611
612                 /* Set root stripeoffset */
613                 sprintf(buf, ".stripeoffset=%hd",
614                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
615                         (typeof(lump->lmm_stripe_offset))(-1));
616                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
617
618 end:
619                 if (param != NULL)
620                         OBD_FREE(param, MGS_PARAM_MAXLEN);
621         }
622         RETURN(rc);
623 }
624
625 /**
626  * This function will be used to get default LOV/LMV/Default LMV
627  * @valid will be used to indicate which stripe it will retrieve
628  *      OBD_MD_MEA              LMV stripe EA
629  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
630  *      otherwise               Default LOV EA.
631  * Each time, it can only retrieve 1 stripe EA
632  **/
633 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
634                      struct ptlrpc_request **request, u64 valid)
635 {
636         struct ll_sb_info *sbi = ll_i2sbi(inode);
637         struct mdt_body   *body;
638         struct lov_mds_md *lmm = NULL;
639         struct ptlrpc_request *req = NULL;
640         int rc, lmm_size;
641         struct md_op_data *op_data;
642         ENTRY;
643
644         rc = ll_get_default_mdsize(sbi, &lmm_size);
645         if (rc)
646                 RETURN(rc);
647
648         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
649                                      0, lmm_size, LUSTRE_OPC_ANY,
650                                      NULL);
651         if (IS_ERR(op_data))
652                 RETURN(PTR_ERR(op_data));
653
654         op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
655         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
656         ll_finish_md_op_data(op_data);
657         if (rc < 0) {
658                 CDEBUG(D_INFO, "md_getattr failed on inode "
659                        DFID": rc %d\n", PFID(ll_inode2fid(inode)), rc);
660                 GOTO(out, rc);
661         }
662
663         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
664         LASSERT(body != NULL);
665
666         lmm_size = body->mbo_eadatasize;
667
668         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
669             lmm_size == 0) {
670                 GOTO(out, rc = -ENODATA);
671         }
672
673         lmm = req_capsule_server_sized_get(&req->rq_pill,
674                                            &RMF_MDT_MD, lmm_size);
675         LASSERT(lmm != NULL);
676
677         /*
678          * This is coming from the MDS, so is probably in
679          * little endian.  We convert it to host endian before
680          * passing it to userspace.
681          */
682         /* We don't swab objects for directories */
683         switch (le32_to_cpu(lmm->lmm_magic)) {
684         case LOV_MAGIC_V1:
685                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
686                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
687                 break;
688         case LOV_MAGIC_V3:
689                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
690                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
691                 break;
692         case LMV_MAGIC_V1:
693                 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
694                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
695                 break;
696         case LMV_USER_MAGIC:
697                 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
698                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
699                 break;
700         default:
701                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
702                 rc = -EPROTO;
703         }
704 out:
705         *plmm = lmm;
706         *plmm_size = lmm_size;
707         *request = req;
708         return rc;
709 }
710
711 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
712 {
713         struct md_op_data       *op_data;
714         int                     rc;
715         int                     mdt_index;
716         ENTRY;
717
718         OBD_ALLOC_PTR(op_data);
719         if (op_data == NULL)
720                 RETURN(-ENOMEM);
721
722         op_data->op_flags |= MF_GET_MDT_IDX;
723         op_data->op_fid1 = *fid;
724         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
725         mdt_index = op_data->op_mds;
726         OBD_FREE_PTR(op_data);
727         if (rc < 0)
728                 RETURN(rc);
729
730         RETURN(mdt_index);
731 }
732
733 /*
734  *  Get MDT index for the inode.
735  */
736 int ll_get_mdt_idx(struct inode *inode)
737 {
738         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
739 }
740
741 /**
742  * Generic handler to do any pre-copy work.
743  *
744  * It sends a first hsm_progress (with extent length == 0) to coordinator as a
745  * first information for it that real work has started.
746  *
747  * Moreover, for a ARCHIVE request, it will sample the file data version and
748  * store it in \a copy.
749  *
750  * \return 0 on success.
751  */
752 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
753 {
754         struct ll_sb_info               *sbi = ll_s2sbi(sb);
755         struct hsm_progress_kernel       hpk;
756         int                              rc = 0;
757         int                              rc2;
758         ENTRY;
759
760         /* Forge a hsm_progress based on data from copy. */
761         hpk.hpk_fid = copy->hc_hai.hai_fid;
762         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
763         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
764         hpk.hpk_extent.length = 0;
765         hpk.hpk_flags = 0;
766         hpk.hpk_errval = 0;
767         hpk.hpk_data_version = 0;
768
769
770         /* For archive request, we need to read the current file version. */
771         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
772                 struct inode    *inode;
773                 __u64            data_version = 0;
774
775                 /* Get inode for this fid */
776                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
777                 if (IS_ERR(inode)) {
778                         hpk.hpk_flags |= HP_FLAG_RETRY;
779                         /* hpk_errval is >= 0 */
780                         hpk.hpk_errval = -PTR_ERR(inode);
781                         GOTO(progress, rc = PTR_ERR(inode));
782                 }
783
784                 /* Read current file data version */
785                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
786                 iput(inode);
787                 if (rc != 0) {
788                         CDEBUG(D_HSM, "Could not read file data version of "
789                                       DFID" (rc = %d). Archive request ("
790                                       LPX64") could not be done.\n",
791                                       PFID(&copy->hc_hai.hai_fid), rc,
792                                       copy->hc_hai.hai_cookie);
793                         hpk.hpk_flags |= HP_FLAG_RETRY;
794                         /* hpk_errval must be >= 0 */
795                         hpk.hpk_errval = -rc;
796                         GOTO(progress, rc);
797                 }
798
799                 /* Store in the hsm_copy for later copytool use.
800                  * Always modified even if no lsm. */
801                 copy->hc_data_version = data_version;
802         }
803
804 progress:
805         /* On error, the request should be considered as completed */
806         if (hpk.hpk_errval > 0)
807                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
808
809         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
810                             &hpk, NULL);
811
812         /* Return first error */
813         RETURN(rc != 0 ? rc : rc2);
814 }
815
816 /**
817  * Generic handler to do any post-copy work.
818  *
819  * It will send the last hsm_progress update to coordinator to inform it
820  * that copy is finished and whether it was successful or not.
821  *
822  * Moreover,
823  * - for ARCHIVE request, it will sample the file data version and compare it
824  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
825  *   will be considered as failed.
826  * - for RESTORE request, it will sample the file data version and send it to
827  *   coordinator which is useful if the file was imported as 'released'.
828  *
829  * \return 0 on success.
830  */
831 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
832 {
833         struct ll_sb_info               *sbi = ll_s2sbi(sb);
834         struct hsm_progress_kernel       hpk;
835         int                              rc = 0;
836         int                              rc2;
837         ENTRY;
838
839         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
840         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
841          * initialized if copy_end was called with copy == NULL.
842          */
843
844         /* Forge a hsm_progress based on data from copy. */
845         hpk.hpk_fid = copy->hc_hai.hai_fid;
846         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
847         hpk.hpk_extent = copy->hc_hai.hai_extent;
848         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
849         hpk.hpk_errval = copy->hc_errval;
850         hpk.hpk_data_version = 0;
851
852         /* For archive request, we need to check the file data was not changed.
853          *
854          * For restore request, we need to send the file data version, this is
855          * useful when the file was created using hsm_import.
856          */
857         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
858              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
859             (copy->hc_errval == 0)) {
860                 struct inode    *inode;
861                 __u64            data_version = 0;
862
863                 /* Get lsm for this fid */
864                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
865                 if (IS_ERR(inode)) {
866                         hpk.hpk_flags |= HP_FLAG_RETRY;
867                         /* hpk_errval must be >= 0 */
868                         hpk.hpk_errval = -PTR_ERR(inode);
869                         GOTO(progress, rc = PTR_ERR(inode));
870                 }
871
872                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
873                 iput(inode);
874                 if (rc) {
875                         CDEBUG(D_HSM, "Could not read file data version. "
876                                       "Request could not be confirmed.\n");
877                         if (hpk.hpk_errval == 0)
878                                 hpk.hpk_errval = -rc;
879                         GOTO(progress, rc);
880                 }
881
882                 /* Store in the hsm_copy for later copytool use.
883                  * Always modified even if no lsm. */
884                 hpk.hpk_data_version = data_version;
885
886                 /* File could have been stripped during archiving, so we need
887                  * to check anyway. */
888                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
889                     (copy->hc_data_version != data_version)) {
890                         CDEBUG(D_HSM, "File data version mismatched. "
891                               "File content was changed during archiving. "
892                                DFID", start:"LPX64" current:"LPX64"\n",
893                                PFID(&copy->hc_hai.hai_fid),
894                                copy->hc_data_version, data_version);
895                         /* File was changed, send error to cdt. Do not ask for
896                          * retry because if a file is modified frequently,
897                          * the cdt will loop on retried archive requests.
898                          * The policy engine will ask for a new archive later
899                          * when the file will not be modified for some tunable
900                          * time */
901                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
902                         rc = -EBUSY;
903                         /* hpk_errval must be >= 0 */
904                         hpk.hpk_errval = -rc;
905                         GOTO(progress, rc);
906                 }
907
908         }
909
910 progress:
911         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
912                             &hpk, NULL);
913
914         /* Return first error */
915         RETURN(rc != 0 ? rc : rc2);
916 }
917
918
919 static int copy_and_ioctl(int cmd, struct obd_export *exp,
920                           const void __user *data, size_t size)
921 {
922         void *copy;
923         int rc;
924
925         OBD_ALLOC(copy, size);
926         if (copy == NULL)
927                 return -ENOMEM;
928
929         if (copy_from_user(copy, data, size)) {
930                 rc = -EFAULT;
931                 goto out;
932         }
933
934         rc = obd_iocontrol(cmd, exp, size, copy, NULL);
935 out:
936         OBD_FREE(copy, size);
937
938         return rc;
939 }
940
941 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
942 {
943         int cmd = qctl->qc_cmd;
944         int type = qctl->qc_type;
945         int id = qctl->qc_id;
946         int valid = qctl->qc_valid;
947         int rc = 0;
948         ENTRY;
949
950         switch (cmd) {
951         case Q_SETQUOTA:
952         case Q_SETINFO:
953                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
954                         RETURN(-EPERM);
955                 break;
956         case Q_GETQUOTA:
957                 if (((type == USRQUOTA &&
958                       !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
959                      (type == GRPQUOTA &&
960                       !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
961                     (!cfs_capable(CFS_CAP_SYS_ADMIN)))
962                         RETURN(-EPERM);
963                 break;
964         case Q_GETINFO:
965                 break;
966         default:
967                 CERROR("unsupported quotactl op: %#x\n", cmd);
968                 RETURN(-ENOTTY);
969         }
970
971         if (valid != QC_GENERAL) {
972                 if (cmd == Q_GETINFO)
973                         qctl->qc_cmd = Q_GETOINFO;
974                 else if (cmd == Q_GETQUOTA)
975                         qctl->qc_cmd = Q_GETOQUOTA;
976                 else
977                         RETURN(-EINVAL);
978
979                 switch (valid) {
980                 case QC_MDTIDX:
981                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
982                                            sizeof(*qctl), qctl, NULL);
983                         break;
984                 case QC_OSTIDX:
985                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
986                                            sizeof(*qctl), qctl, NULL);
987                         break;
988                 case QC_UUID:
989                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
990                                            sizeof(*qctl), qctl, NULL);
991                         if (rc == -EAGAIN)
992                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
993                                                    sbi->ll_dt_exp,
994                                                    sizeof(*qctl), qctl, NULL);
995                         break;
996                 default:
997                         rc = -EINVAL;
998                         break;
999                 }
1000
1001                 if (rc)
1002                         RETURN(rc);
1003
1004                 qctl->qc_cmd = cmd;
1005         } else {
1006                 struct obd_quotactl *oqctl;
1007
1008                 OBD_ALLOC_PTR(oqctl);
1009                 if (oqctl == NULL)
1010                         RETURN(-ENOMEM);
1011
1012                 QCTL_COPY(oqctl, qctl);
1013                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1014                 if (rc) {
1015                         OBD_FREE_PTR(oqctl);
1016                         RETURN(rc);
1017                 }
1018                 /* If QIF_SPACE is not set, client should collect the
1019                  * space usage from OSSs by itself */
1020                 if (cmd == Q_GETQUOTA &&
1021                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1022                     !oqctl->qc_dqblk.dqb_curspace) {
1023                         struct obd_quotactl *oqctl_tmp;
1024
1025                         OBD_ALLOC_PTR(oqctl_tmp);
1026                         if (oqctl_tmp == NULL)
1027                                 GOTO(out, rc = -ENOMEM);
1028
1029                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1030                         oqctl_tmp->qc_id = oqctl->qc_id;
1031                         oqctl_tmp->qc_type = oqctl->qc_type;
1032
1033                         /* collect space usage from OSTs */
1034                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1035                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1036                         if (!rc || rc == -EREMOTEIO) {
1037                                 oqctl->qc_dqblk.dqb_curspace =
1038                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1039                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1040                         }
1041
1042                         /* collect space & inode usage from MDTs */
1043                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1044                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1045                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1046                         if (!rc || rc == -EREMOTEIO) {
1047                                 oqctl->qc_dqblk.dqb_curspace +=
1048                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1049                                 oqctl->qc_dqblk.dqb_curinodes =
1050                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1051                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1052                         } else {
1053                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1054                         }
1055
1056                         OBD_FREE_PTR(oqctl_tmp);
1057                 }
1058 out:
1059                 QCTL_COPY(qctl, oqctl);
1060                 OBD_FREE_PTR(oqctl);
1061         }
1062
1063         RETURN(rc);
1064 }
1065
1066 /* This function tries to get a single name component,
1067  * to send to the server. No actual path traversal involved,
1068  * so we limit to NAME_MAX */
1069 static char *ll_getname(const char __user *filename)
1070 {
1071         int ret = 0, len;
1072         char *tmp;
1073
1074         OBD_ALLOC(tmp, NAME_MAX + 1);
1075
1076         if (!tmp)
1077                 return ERR_PTR(-ENOMEM);
1078
1079         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1080         if (len < 0)
1081                 ret = -ENOENT;
1082         else if (len > NAME_MAX)
1083                 ret = -ENAMETOOLONG;
1084
1085         if (ret) {
1086                 OBD_FREE(tmp, NAME_MAX + 1);
1087                 tmp =  ERR_PTR(ret);
1088         }
1089         return tmp;
1090 }
1091
1092 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1093
1094 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1095 {
1096         struct inode *inode = file_inode(file);
1097         struct ll_sb_info *sbi = ll_i2sbi(inode);
1098         struct obd_ioctl_data *data;
1099         int rc = 0;
1100         ENTRY;
1101
1102         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1103                PFID(ll_inode2fid(inode)), inode, cmd);
1104
1105         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1106         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1107                 return -ENOTTY;
1108
1109         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1110         switch(cmd) {
1111         case FSFILT_IOC_GETFLAGS:
1112         case FSFILT_IOC_SETFLAGS:
1113                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1114         case FSFILT_IOC_GETVERSION_OLD:
1115         case FSFILT_IOC_GETVERSION:
1116                 RETURN(put_user(inode->i_generation, (int __user *)arg));
1117         /* We need to special case any other ioctls we want to handle,
1118          * to send them to the MDS/OST as appropriate and to properly
1119          * network encode the arg field.
1120         case FSFILT_IOC_SETVERSION_OLD:
1121         case FSFILT_IOC_SETVERSION:
1122         */
1123         case LL_IOC_GET_MDTIDX: {
1124                 int mdtidx;
1125
1126                 mdtidx = ll_get_mdt_idx(inode);
1127                 if (mdtidx < 0)
1128                         RETURN(mdtidx);
1129
1130                 if (put_user((int)mdtidx, (int __user *)arg))
1131                         RETURN(-EFAULT);
1132
1133                 return 0;
1134         }
1135         case IOC_MDC_LOOKUP: {
1136                 int namelen, len = 0;
1137                 char *buf = NULL;
1138                 char *filename;
1139
1140                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1141                 if (rc != 0)
1142                         RETURN(rc);
1143                 data = (void *)buf;
1144
1145                 filename = data->ioc_inlbuf1;
1146                 namelen = strlen(filename);
1147                 if (namelen < 1) {
1148                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1149                         GOTO(out_free, rc = -EINVAL);
1150                 }
1151
1152                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1153                 if (rc < 0) {
1154                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1155                                ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1156                                filename, rc);
1157                         GOTO(out_free, rc);
1158                 }
1159 out_free:
1160                 obd_ioctl_freedata(buf, len);
1161                 return rc;
1162         }
1163         case LL_IOC_LMV_SETSTRIPE: {
1164                 struct lmv_user_md  *lum;
1165                 char            *buf = NULL;
1166                 char            *filename;
1167                 int              namelen = 0;
1168                 int              lumlen = 0;
1169                 umode_t          mode;
1170                 int              len;
1171                 int              rc;
1172
1173                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1174                 if (rc)
1175                         RETURN(rc);
1176
1177                 data = (void *)buf;
1178                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1179                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1180                         GOTO(lmv_out_free, rc = -EINVAL);
1181
1182                 filename = data->ioc_inlbuf1;
1183                 namelen = data->ioc_inllen1;
1184
1185                 if (namelen < 1) {
1186                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1187                         GOTO(lmv_out_free, rc = -EINVAL);
1188                 }
1189                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1190                 lumlen = data->ioc_inllen2;
1191
1192                 if (lum->lum_magic != LMV_USER_MAGIC ||
1193                     lumlen != sizeof(*lum)) {
1194                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1195                                filename, lum->lum_magic, lumlen, -EFAULT);
1196                         GOTO(lmv_out_free, rc = -EINVAL);
1197                 }
1198
1199 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 50, 0)
1200                 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1201 #else
1202                 mode = data->ioc_type;
1203 #endif
1204                 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
1205 lmv_out_free:
1206                 obd_ioctl_freedata(buf, len);
1207                 RETURN(rc);
1208
1209         }
1210         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1211                 struct lmv_user_md        lum;
1212                 struct lmv_user_md __user *ulump =
1213                                         (struct lmv_user_md __user *)arg;
1214                 int                       rc;
1215
1216                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1217                         RETURN(-EFAULT);
1218
1219                 if (lum.lum_magic != LMV_USER_MAGIC)
1220                         RETURN(-EINVAL);
1221
1222                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1223
1224                 RETURN(rc);
1225         }
1226         case LL_IOC_LOV_SETSTRIPE: {
1227                 struct lov_user_md_v3 lumv3;
1228                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1229                 struct lov_user_md_v1 __user *lumv1p =
1230                         (struct lov_user_md_v1 __user *)arg;
1231                 struct lov_user_md_v3 __user *lumv3p =
1232                         (struct lov_user_md_v3 __user *)arg;
1233
1234                 int set_default = 0;
1235
1236                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1237                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1238                         sizeof(lumv3p->lmm_objects[0]));
1239                 /* first try with v1 which is smaller than v3 */
1240                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1241                         RETURN(-EFAULT);
1242
1243                 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1244                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1245                                 RETURN(-EFAULT);
1246                 }
1247
1248                 if (inode->i_sb->s_root == file_dentry(file))
1249                         set_default = 1;
1250
1251                 /* in v1 and v3 cases lumv1 points to data */
1252                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1253
1254                 RETURN(rc);
1255         }
1256         case LL_IOC_LMV_GETSTRIPE: {
1257                 struct lmv_user_md __user *ulmv =
1258                                         (struct lmv_user_md __user *)arg;
1259                 struct lmv_user_md      lum;
1260                 struct ptlrpc_request   *request = NULL;
1261                 union lmv_mds_md        *lmm = NULL;
1262                 int                     lmmsize;
1263                 u64                     valid = 0;
1264                 struct lmv_user_md      *tmp = NULL;
1265                 int                     mdt_index;
1266                 int                     lum_size;
1267                 int                     stripe_count;
1268                 int                     max_stripe_count;
1269                 int                     i;
1270                 int                     rc;
1271
1272                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1273                         RETURN(-EFAULT);
1274
1275                 max_stripe_count = lum.lum_stripe_count;
1276                 /* lum_magic will indicate which stripe the ioctl will like
1277                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1278                  * is for default LMV stripe */
1279                 if (lum.lum_magic == LMV_MAGIC_V1)
1280                         valid |= OBD_MD_MEA;
1281                 else if (lum.lum_magic == LMV_USER_MAGIC)
1282                         valid |= OBD_MD_DEFAULT_MEA;
1283                 else
1284                         RETURN(-EINVAL);
1285
1286                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1287                                       valid);
1288                 if (rc != 0)
1289                         GOTO(finish_req, rc);
1290
1291                 /* Get default LMV EA */
1292                 if (lum.lum_magic == LMV_USER_MAGIC) {
1293                         if (lmmsize > sizeof(*ulmv))
1294                                 GOTO(finish_req, rc = -EINVAL);
1295
1296                         if (copy_to_user(ulmv, lmm, lmmsize))
1297                                 GOTO(finish_req, rc = -EFAULT);
1298
1299                         GOTO(finish_req, rc);
1300                 }
1301
1302                 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1303                 if (max_stripe_count < stripe_count) {
1304                         lum.lum_stripe_count = stripe_count;
1305                         if (copy_to_user(ulmv, &lum, sizeof(lum)))
1306                                 GOTO(finish_req, rc = -EFAULT);
1307                         GOTO(finish_req, rc = -E2BIG);
1308                 }
1309
1310                 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1311                 OBD_ALLOC(tmp, lum_size);
1312                 if (tmp == NULL)
1313                         GOTO(finish_req, rc = -ENOMEM);
1314
1315                 mdt_index = ll_get_mdt_idx(inode);
1316                 if (mdt_index < 0)
1317                         GOTO(out_tmp, rc = -ENOMEM);
1318
1319                 tmp->lum_magic = LMV_MAGIC_V1;
1320                 tmp->lum_stripe_count = 0;
1321                 tmp->lum_stripe_offset = mdt_index;
1322                 for (i = 0; i < stripe_count; i++) {
1323                         struct lu_fid   fid;
1324
1325                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1326                         mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1327                         if (mdt_index < 0)
1328                                 GOTO(out_tmp, rc = mdt_index);
1329
1330                         tmp->lum_objects[i].lum_mds = mdt_index;
1331                         tmp->lum_objects[i].lum_fid = fid;
1332                         tmp->lum_stripe_count++;
1333                 }
1334
1335                 if (copy_to_user(ulmv, tmp, lum_size))
1336                         GOTO(out_tmp, rc = -EFAULT);
1337 out_tmp:
1338                 OBD_FREE(tmp, lum_size);
1339 finish_req:
1340                 ptlrpc_req_finished(request);
1341                 return rc;
1342         }
1343
1344         case LL_IOC_REMOVE_ENTRY: {
1345                 char            *filename = NULL;
1346                 int              namelen = 0;
1347                 int              rc;
1348
1349                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1350                  * unsupported server, which might crash the server(LU-2730),
1351                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1352                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1353                  * server will support REINT_RMENTRY XXX*/
1354                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1355                         RETURN(-ENOTSUPP);
1356
1357                 filename = ll_getname((const char __user *)arg);
1358                 if (IS_ERR(filename))
1359                         RETURN(PTR_ERR(filename));
1360
1361                 namelen = strlen(filename);
1362                 if (namelen < 1)
1363                         GOTO(out_rmdir, rc = -EINVAL);
1364
1365                 rc = ll_rmdir_entry(inode, filename, namelen);
1366 out_rmdir:
1367                 if (filename)
1368                         ll_putname(filename);
1369                 RETURN(rc);
1370         }
1371         case LL_IOC_LOV_SWAP_LAYOUTS:
1372                 RETURN(-EPERM);
1373         case IOC_OBD_STATFS:
1374                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1375         case LL_IOC_LOV_GETSTRIPE:
1376         case LL_IOC_MDC_GETINFO:
1377         case IOC_MDC_GETFILEINFO:
1378         case IOC_MDC_GETFILESTRIPE: {
1379                 struct ptlrpc_request *request = NULL;
1380                 struct lov_user_md __user *lump;
1381                 struct lov_mds_md *lmm = NULL;
1382                 struct mdt_body *body;
1383                 char *filename = NULL;
1384                 int lmmsize;
1385
1386                 if (cmd == IOC_MDC_GETFILEINFO ||
1387                     cmd == IOC_MDC_GETFILESTRIPE) {
1388                         filename = ll_getname((const char __user *)arg);
1389                         if (IS_ERR(filename))
1390                                 RETURN(PTR_ERR(filename));
1391
1392                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1393                                                       &lmmsize, &request);
1394                 } else {
1395                         rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1396                                               &request, 0);
1397                 }
1398
1399                 if (request) {
1400                         body = req_capsule_server_get(&request->rq_pill,
1401                                                       &RMF_MDT_BODY);
1402                         LASSERT(body != NULL);
1403                 } else {
1404                         GOTO(out_req, rc);
1405                 }
1406
1407                 if (rc < 0) {
1408                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1409                                                cmd == LL_IOC_MDC_GETINFO))
1410                                 GOTO(skip_lmm, rc = 0);
1411                         else
1412                                 GOTO(out_req, rc);
1413                 }
1414
1415                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1416                     cmd == LL_IOC_LOV_GETSTRIPE) {
1417                         lump = (struct lov_user_md __user *)arg;
1418                 } else {
1419                         struct lov_user_mds_data __user *lmdp;
1420                         lmdp = (struct lov_user_mds_data __user *)arg;
1421                         lump = &lmdp->lmd_lmm;
1422                 }
1423                 if (copy_to_user(lump, lmm, lmmsize)) {
1424                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1425                                 GOTO(out_req, rc = -EFAULT);
1426                         rc = -EOVERFLOW;
1427                 }
1428         skip_lmm:
1429                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1430                         struct lov_user_mds_data __user *lmdp;
1431                         lstat_t st = { 0 };
1432
1433                         st.st_dev       = inode->i_sb->s_dev;
1434                         st.st_mode      = body->mbo_mode;
1435                         st.st_nlink     = body->mbo_nlink;
1436                         st.st_uid       = body->mbo_uid;
1437                         st.st_gid       = body->mbo_gid;
1438                         st.st_rdev      = body->mbo_rdev;
1439                         st.st_size      = body->mbo_size;
1440                         st.st_blksize   = PAGE_CACHE_SIZE;
1441                         st.st_blocks    = body->mbo_blocks;
1442                         st.st_atime     = body->mbo_atime;
1443                         st.st_mtime     = body->mbo_mtime;
1444                         st.st_ctime     = body->mbo_ctime;
1445                         st.st_ino       = cl_fid_build_ino(&body->mbo_fid1,
1446                                                 sbi->ll_flags &
1447                                                 LL_SBI_32BIT_API);
1448
1449                         lmdp = (struct lov_user_mds_data __user *)arg;
1450                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1451                                 GOTO(out_req, rc = -EFAULT);
1452                 }
1453
1454                 EXIT;
1455         out_req:
1456                 ptlrpc_req_finished(request);
1457                 if (filename)
1458                         ll_putname(filename);
1459                 return rc;
1460         }
1461         case OBD_IOC_QUOTACTL: {
1462                 struct if_quotactl *qctl;
1463
1464                 OBD_ALLOC_PTR(qctl);
1465                 if (!qctl)
1466                         RETURN(-ENOMEM);
1467
1468                 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1469                         GOTO(out_quotactl, rc = -EFAULT);
1470
1471                 rc = quotactl_ioctl(sbi, qctl);
1472
1473                 if (rc == 0 &&
1474                     copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1475                         rc = -EFAULT;
1476
1477         out_quotactl:
1478                 OBD_FREE_PTR(qctl);
1479                 RETURN(rc);
1480         }
1481         case OBD_IOC_GETDTNAME:
1482         case OBD_IOC_GETMDNAME:
1483                 RETURN(ll_get_obd_name(inode, cmd, arg));
1484         case LL_IOC_FLUSHCTX:
1485                 RETURN(ll_flush_ctx(inode));
1486         case LL_IOC_GETOBDCOUNT: {
1487                 u32 count, vallen;
1488                 struct obd_export *exp;
1489
1490                 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1491                         RETURN(-EFAULT);
1492
1493                 /* get ost count when count is zero, get mdt count otherwise */
1494                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1495                 vallen = sizeof(count);
1496                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1497                                   KEY_TGT_COUNT, &vallen, &count);
1498                 if (rc) {
1499                         CERROR("get target count failed: %d\n", rc);
1500                         RETURN(rc);
1501                 }
1502
1503                 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1504                         RETURN(-EFAULT);
1505
1506                 RETURN(0);
1507         }
1508         case LL_IOC_PATH2FID:
1509                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1510                                      sizeof(struct lu_fid)))
1511                         RETURN(-EFAULT);
1512                 RETURN(0);
1513         case LL_IOC_GET_CONNECT_FLAGS: {
1514                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1515                                      (void __user *)arg));
1516         }
1517         case OBD_IOC_CHANGELOG_SEND:
1518         case OBD_IOC_CHANGELOG_CLEAR:
1519                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1520                         RETURN(-EPERM);
1521
1522                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1523                                     sizeof(struct ioc_changelog));
1524                 RETURN(rc);
1525         case OBD_IOC_FID2PATH:
1526                 RETURN(ll_fid2path(inode, (void __user *)arg));
1527         case LL_IOC_GETPARENT:
1528                 RETURN(ll_getparent(file, (void __user *)arg));
1529         case LL_IOC_FID2MDTIDX: {
1530                 struct obd_export *exp = ll_i2mdexp(inode);
1531                 struct lu_fid     fid;
1532                 __u32             index;
1533
1534                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1535                                    sizeof(fid)))
1536                         RETURN(-EFAULT);
1537
1538                 /* Call mdc_iocontrol */
1539                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1540                                    (__u32 __user *)&index);
1541                 if (rc != 0)
1542                         RETURN(rc);
1543
1544                 RETURN(index);
1545         }
1546         case LL_IOC_HSM_REQUEST: {
1547                 struct hsm_user_request *hur;
1548                 ssize_t                  totalsize;
1549
1550                 OBD_ALLOC_PTR(hur);
1551                 if (hur == NULL)
1552                         RETURN(-ENOMEM);
1553
1554                 /* We don't know the true size yet; copy the fixed-size part */
1555                 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1556                         OBD_FREE_PTR(hur);
1557                         RETURN(-EFAULT);
1558                 }
1559
1560                 /* Compute the whole struct size */
1561                 totalsize = hur_len(hur);
1562                 OBD_FREE_PTR(hur);
1563                 if (totalsize < 0)
1564                         RETURN(-E2BIG);
1565
1566                 /* Final size will be more than double totalsize */
1567                 if (totalsize >= MDS_MAXREQSIZE / 3)
1568                         RETURN(-E2BIG);
1569
1570                 OBD_ALLOC_LARGE(hur, totalsize);
1571                 if (hur == NULL)
1572                         RETURN(-ENOMEM);
1573
1574                 /* Copy the whole struct */
1575                 if (copy_from_user(hur, (void __user *)arg, totalsize))
1576                         GOTO(out_hur, rc = -EFAULT);
1577
1578                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1579                         const struct lu_fid *fid;
1580                         struct inode *f;
1581                         int i;
1582
1583                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1584                                 fid = &hur->hur_user_item[i].hui_fid;
1585                                 f = search_inode_for_lustre(inode->i_sb, fid);
1586                                 if (IS_ERR(f)) {
1587                                         rc = PTR_ERR(f);
1588                                         break;
1589                                 }
1590
1591                                 rc = ll_hsm_release(f);
1592                                 iput(f);
1593                                 if (rc != 0)
1594                                         break;
1595                         }
1596                 } else {
1597                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1598                                            hur, NULL);
1599                 }
1600
1601 out_hur:
1602                 OBD_FREE_LARGE(hur, totalsize);
1603
1604                 RETURN(rc);
1605         }
1606         case LL_IOC_HSM_PROGRESS: {
1607                 struct hsm_progress_kernel      hpk;
1608                 struct hsm_progress             hp;
1609
1610                 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1611                         RETURN(-EFAULT);
1612
1613                 hpk.hpk_fid = hp.hp_fid;
1614                 hpk.hpk_cookie = hp.hp_cookie;
1615                 hpk.hpk_extent = hp.hp_extent;
1616                 hpk.hpk_flags = hp.hp_flags;
1617                 hpk.hpk_errval = hp.hp_errval;
1618                 hpk.hpk_data_version = 0;
1619
1620                 /* File may not exist in Lustre; all progress
1621                  * reported to Lustre root */
1622                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1623                                    NULL);
1624                 RETURN(rc);
1625         }
1626         case LL_IOC_HSM_CT_START:
1627                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1628                         RETURN(-EPERM);
1629
1630                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1631                                     sizeof(struct lustre_kernelcomm));
1632                 RETURN(rc);
1633
1634         case LL_IOC_HSM_COPY_START: {
1635                 struct hsm_copy *copy;
1636                 int              rc;
1637
1638                 OBD_ALLOC_PTR(copy);
1639                 if (copy == NULL)
1640                         RETURN(-ENOMEM);
1641                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
1642                         OBD_FREE_PTR(copy);
1643                         RETURN(-EFAULT);
1644                 }
1645
1646                 rc = ll_ioc_copy_start(inode->i_sb, copy);
1647                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1648                         rc = -EFAULT;
1649
1650                 OBD_FREE_PTR(copy);
1651                 RETURN(rc);
1652         }
1653         case LL_IOC_HSM_COPY_END: {
1654                 struct hsm_copy *copy;
1655                 int              rc;
1656
1657                 OBD_ALLOC_PTR(copy);
1658                 if (copy == NULL)
1659                         RETURN(-ENOMEM);
1660                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
1661                         OBD_FREE_PTR(copy);
1662                         RETURN(-EFAULT);
1663                 }
1664
1665                 rc = ll_ioc_copy_end(inode->i_sb, copy);
1666                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1667                         rc = -EFAULT;
1668
1669                 OBD_FREE_PTR(copy);
1670                 RETURN(rc);
1671         }
1672         case LL_IOC_MIGRATE: {
1673                 char            *buf = NULL;
1674                 const char      *filename;
1675                 int             namelen = 0;
1676                 int             len;
1677                 int             rc;
1678                 int             mdtidx;
1679
1680                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1681                 if (rc < 0)
1682                         RETURN(rc);
1683
1684                 data = (struct obd_ioctl_data *)buf;
1685                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1686                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1687                         GOTO(migrate_free, rc = -EINVAL);
1688
1689                 filename = data->ioc_inlbuf1;
1690                 namelen = data->ioc_inllen1;
1691                 /* \0 is packed at the end of filename */
1692                 if (namelen < 1 || namelen != strlen(filename) + 1)
1693                         GOTO(migrate_free, rc = -EINVAL);
1694
1695                 if (data->ioc_inllen2 != sizeof(mdtidx))
1696                         GOTO(migrate_free, rc = -EINVAL);
1697                 mdtidx = *(int *)data->ioc_inlbuf2;
1698
1699                 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
1700 migrate_free:
1701                 obd_ioctl_freedata(buf, len);
1702
1703                 RETURN(rc);
1704         }
1705         default:
1706                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1707                                      (void __user *)arg));
1708         }
1709 }
1710
1711 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1712 {
1713         struct inode *inode = file->f_mapping->host;
1714         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1715         struct ll_sb_info *sbi = ll_i2sbi(inode);
1716         int api32 = ll_need_32bit_api(sbi);
1717         loff_t ret = -EINVAL;
1718         ENTRY;
1719
1720         inode_lock(inode);
1721         switch (origin) {
1722                 case SEEK_SET:
1723                         break;
1724                 case SEEK_CUR:
1725                         offset += file->f_pos;
1726                         break;
1727                 case SEEK_END:
1728                         if (offset > 0)
1729                                 GOTO(out, ret);
1730                         if (api32)
1731                                 offset += LL_DIR_END_OFF_32BIT;
1732                         else
1733                                 offset += LL_DIR_END_OFF;
1734                         break;
1735                 default:
1736                         GOTO(out, ret);
1737         }
1738
1739         if (offset >= 0 &&
1740             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1741              (!api32 && offset <= LL_DIR_END_OFF))) {
1742                 if (offset != file->f_pos) {
1743                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1744                             (!api32 && offset == LL_DIR_END_OFF))
1745                                 fd->lfd_pos = MDS_DIR_END_OFF;
1746                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1747                                 fd->lfd_pos = offset << 32;
1748                         else
1749                                 fd->lfd_pos = offset;
1750                         file->f_pos = offset;
1751                         file->f_version = 0;
1752                 }
1753                 ret = offset;
1754         }
1755         GOTO(out, ret);
1756
1757 out:
1758         inode_unlock(inode);
1759         return ret;
1760 }
1761
1762 static int ll_dir_open(struct inode *inode, struct file *file)
1763 {
1764         ENTRY;
1765         RETURN(ll_file_open(inode, file));
1766 }
1767
1768 static int ll_dir_release(struct inode *inode, struct file *file)
1769 {
1770         ENTRY;
1771         RETURN(ll_file_release(inode, file));
1772 }
1773
1774 const struct file_operations ll_dir_operations = {
1775         .llseek         = ll_dir_seek,
1776         .open           = ll_dir_open,
1777         .release        = ll_dir_release,
1778         .read           = generic_read_dir,
1779 #ifdef HAVE_DIR_CONTEXT
1780         .iterate        = ll_iterate,
1781 #else
1782         .readdir        = ll_readdir,
1783 #endif
1784         .unlocked_ioctl = ll_dir_ioctl,
1785         .fsync          = ll_fsync,
1786 };