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