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