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