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