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