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