Whamcloud - gitweb
b=14149
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002-2005 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30
31 #include <lustre_lite.h>
32 #include <lustre_ha.h>
33 #include <lustre_dlm.h>
34 #include <lprocfs_status.h>
35 #include <lustre_disk.h>
36 #include <lustre_param.h>
37 #include <lustre_log.h>
38 #include "llite_internal.h"
39
40 cfs_mem_cache_t *ll_file_data_slab;
41
42 LIST_HEAD(ll_super_blocks);
43 spinlock_t ll_sb_lock = SPIN_LOCK_UNLOCKED;
44
45 extern struct address_space_operations ll_aops;
46 extern struct address_space_operations ll_dir_aops;
47
48 #ifndef log2
49 #define log2(n) ffz(~(n))
50 #endif
51
52
53 static struct ll_sb_info *ll_init_sbi(void)
54 {
55         struct ll_sb_info *sbi = NULL;
56         class_uuid_t uuid;
57         int i;
58         ENTRY;
59
60         OBD_ALLOC(sbi, sizeof(*sbi));
61         if (!sbi)
62                 RETURN(NULL);
63
64         spin_lock_init(&sbi->ll_lock);
65         spin_lock_init(&sbi->ll_lco.lco_lock);
66         spin_lock_init(&sbi->ll_pp_extent_lock);
67         spin_lock_init(&sbi->ll_process_lock);
68         sbi->ll_rw_stats_on = 0;
69         INIT_LIST_HEAD(&sbi->ll_pglist);
70         if (num_physpages >> (20 - CFS_PAGE_SHIFT) < 512)
71                 sbi->ll_async_page_max = num_physpages / 2;
72         else
73                 sbi->ll_async_page_max = (num_physpages / 4) * 3;
74         sbi->ll_ra_info.ra_max_pages = min(num_physpages / 8,
75                                            SBI_DEFAULT_READAHEAD_MAX);
76         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
77                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
78
79         INIT_LIST_HEAD(&sbi->ll_conn_chain);
80         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
81
82         ll_generate_random_uuid(uuid);
83         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
84         CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
85
86         spin_lock(&ll_sb_lock);
87         list_add_tail(&sbi->ll_list, &ll_super_blocks);
88         spin_unlock(&ll_sb_lock);
89
90 #ifdef ENABLE_LLITE_CHECKSUM
91         sbi->ll_flags |= LL_SBI_CHECKSUM;
92 #endif
93
94 #ifdef HAVE_LRU_RESIZE_SUPPORT
95         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
96 #endif
97
98 #ifdef HAVE_EXPORT___IGET
99         INIT_LIST_HEAD(&sbi->ll_deathrow);
100         spin_lock_init(&sbi->ll_deathrow_lock);
101 #endif
102         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
103                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_r_hist.oh_lock);
104                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_w_hist.oh_lock);
105         }
106
107         RETURN(sbi);
108 }
109
110 void ll_free_sbi(struct super_block *sb)
111 {
112         struct ll_sb_info *sbi = ll_s2sbi(sb);
113         ENTRY;
114
115         if (sbi != NULL) {
116                 spin_lock(&ll_sb_lock);
117                 list_del(&sbi->ll_list);
118                 spin_unlock(&ll_sb_lock);
119                 OBD_FREE(sbi, sizeof(*sbi));
120         }
121         EXIT;
122 }
123
124 static struct dentry_operations ll_d_root_ops = {
125 #ifdef DCACHE_LUSTRE_INVALID
126         .d_compare = ll_dcompare,
127 #endif
128 };
129
130 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
131  * us to make MDS RPCs with large enough reply buffers to hold the
132  * maximum-sized (= maximum striped) EA and cookie without having to
133  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
134 static int ll_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
135 {
136         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
137         __u32 valsize = sizeof(struct lov_desc);
138         int rc, easize, def_easize, cookiesize;
139         struct lov_desc desc;
140         __u32 stripes;
141         ENTRY;
142
143         rc = obd_get_info(dt_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
144                           &valsize, &desc);
145         if (rc)
146                 RETURN(rc);
147
148         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
149         lsm.lsm_stripe_count = stripes;
150         easize = obd_size_diskmd(dt_exp, &lsm);
151
152         lsm.lsm_stripe_count = desc.ld_default_stripe_count;
153         def_easize = obd_size_diskmd(dt_exp, &lsm);
154
155         cookiesize = stripes * sizeof(struct llog_cookie);
156
157         CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
158                easize, cookiesize);
159
160         rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
161         RETURN(rc);
162 }
163
164 static int client_common_fill_super(struct super_block *sb, char *md, char *dt)
165 {
166         struct inode *root = 0;
167         struct ll_sb_info *sbi = ll_s2sbi(sb);
168         struct obd_device *obd;
169         struct lu_fid rootfid;
170         struct obd_capa *oc = NULL;
171         struct obd_statfs osfs;
172         struct ptlrpc_request *request = NULL;
173         struct lustre_handle dt_conn = {0, };
174         struct lustre_handle md_conn = {0, };
175         struct obd_connect_data *data = NULL;
176         struct lustre_md lmd;
177         obd_valid valid;
178         int size, err, checksum;
179         ENTRY;
180
181         obd = class_name2obd(md);
182         if (!obd) {
183                 CERROR("MD %s: not setup or attached\n", md);
184                 RETURN(-EINVAL);
185         }
186
187         OBD_ALLOC_PTR(data);
188         if (data == NULL)
189                 RETURN(-ENOMEM);
190
191         if (proc_lustre_fs_root) {
192                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
193                                                   dt, md);
194                 if (err < 0)
195                         CERROR("could not register mount in /proc/lustre");
196         }
197
198         /* indicate the features supported by this client */
199         data->ocd_connect_flags = OBD_CONNECT_IBITS    | OBD_CONNECT_NODEVOH  |
200                                   OBD_CONNECT_JOIN     | OBD_CONNECT_ATTRFID  |
201                                   OBD_CONNECT_VERSION  | OBD_CONNECT_MDS_CAPA |
202                                   OBD_CONNECT_OSS_CAPA | OBD_CONNECT_CANCELSET|
203                                   OBD_CONNECT_FID;
204
205 #ifdef HAVE_LRU_RESIZE_SUPPORT
206         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
207                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
208 #endif
209 #ifdef CONFIG_FS_POSIX_ACL
210         data->ocd_connect_flags |= OBD_CONNECT_ACL;
211 #endif
212         data->ocd_ibits_known = MDS_INODELOCK_FULL;
213         data->ocd_version = LUSTRE_VERSION_CODE;
214
215         if (sb->s_flags & MS_RDONLY)
216                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
217         if (sbi->ll_flags & LL_SBI_USER_XATTR)
218                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
219
220 #ifdef HAVE_MS_FLOCK_LOCK
221         /* force vfs to use lustre handler for flock() calls - bug 10743 */
222         sb->s_flags |= MS_FLOCK_LOCK;
223 #endif
224         
225         if (sbi->ll_flags & LL_SBI_FLOCK)
226                 sbi->ll_fop = &ll_file_operations_flock;
227         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
228                 sbi->ll_fop = &ll_file_operations;
229         else
230                 sbi->ll_fop = &ll_file_operations_noflock;
231
232         /* real client */
233         data->ocd_connect_flags |= OBD_CONNECT_REAL;
234         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
235                 data->ocd_connect_flags &= ~OBD_CONNECT_LCL_CLIENT;
236                 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT;
237         } else {
238                 data->ocd_connect_flags &= ~OBD_CONNECT_RMT_CLIENT;
239                 data->ocd_connect_flags |= OBD_CONNECT_LCL_CLIENT;
240         }
241
242         err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, data);
243         if (err == -EBUSY) {
244                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (md %s) is performing "
245                                    "recovery, of which this client is not a "
246                                    "part. Please wait for recovery to complete,"
247                                    " abort, or time out.\n", md);
248                 GOTO(out, err);
249         } else if (err) {
250                 CERROR("cannot connect to %s: rc = %d\n", md, err);
251                 GOTO(out, err);
252         }
253         sbi->ll_md_exp = class_conn2export(&md_conn);
254
255         err = obd_fid_init(sbi->ll_md_exp);
256         if (err) {
257                 CERROR("Can't init metadata layer FID infrastructure, "
258                        "rc %d\n", err);
259                 GOTO(out_md, err);
260         }
261
262         err = obd_statfs(obd, &osfs, cfs_time_current_64() - HZ);
263         if (err)
264                 GOTO(out_md_fid, err);
265
266         size = sizeof(*data);
267         err = obd_get_info(sbi->ll_md_exp, strlen(KEY_CONN_DATA),
268                            KEY_CONN_DATA,  &size, data);
269         if (err) {
270                 CERROR("Get connect data failed: %d \n", err);
271                 GOTO(out_md, err);
272         }
273
274         LASSERT(osfs.os_bsize);
275         sb->s_blocksize = osfs.os_bsize;
276         sb->s_blocksize_bits = log2(osfs.os_bsize);
277         sb->s_magic = LL_SUPER_MAGIC;
278
279         /* for bug 11559. in $LINUX/fs/read_write.c, function do_sendfile():
280          *         retval = in_file->f_op->sendfile(...);
281          *         if (*ppos > max)
282          *                 retval = -EOVERFLOW;
283          *
284          * it will check if *ppos is greater than max. However, max equals to
285          * s_maxbytes, which is a negative integer in a x86_64 box since loff_t
286          * has been defined as a signed long long ineger in linux kernel. */
287 #if BITS_PER_LONG == 64
288         sb->s_maxbytes = PAGE_CACHE_MAXBYTES >> 1;
289 #else
290         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
291 #endif
292         sbi->ll_namelen = osfs.os_namelen;
293         sbi->ll_max_rw_chunk = LL_DEFAULT_MAX_RW_CHUNK;
294
295         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
296             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
297                 LCONSOLE_INFO("Disabling user_xattr feature because "
298                               "it is not supported on the server\n");
299                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
300         }
301
302         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
303 #ifdef MS_POSIXACL
304                 sb->s_flags |= MS_POSIXACL;
305 #endif
306                 sbi->ll_flags |= LL_SBI_ACL;
307         } else {
308                 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
309 #ifdef MS_POSIXACL
310                 sb->s_flags &= ~MS_POSIXACL;
311 #endif
312                 sbi->ll_flags &= ~LL_SBI_ACL;
313         }
314
315         if (data->ocd_connect_flags & OBD_CONNECT_JOIN)
316                 sbi->ll_flags |= LL_SBI_JOIN;
317
318         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
319                 if (!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT)) {
320                         /* sometimes local client claims to be remote, but mdt
321                          * will disagree when client gss not applied. */
322                         LCONSOLE_INFO("client claims to be remote, but server "
323                                       "rejected, forced to be local.\n");
324                         sbi->ll_flags &= ~LL_SBI_RMT_CLIENT;
325                 }
326         } else {
327                 if (!(data->ocd_connect_flags & OBD_CONNECT_LCL_CLIENT)) {
328                         /* with gss applied, remote client can not claim to be
329                          * local, so mdt maybe force client to be remote. */
330                         LCONSOLE_INFO("client claims to be local, but server "
331                                       "rejected, forced to be remote.\n");
332                         sbi->ll_flags |= LL_SBI_RMT_CLIENT;
333                 }
334         }
335
336         if (data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) {
337                 LCONSOLE_INFO("client enabled MDS capability!\n");
338                 sbi->ll_flags |= LL_SBI_MDS_CAPA;
339         }
340
341         if (data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA) {
342                 LCONSOLE_INFO("client enabled OSS capability!\n");
343                 sbi->ll_flags |= LL_SBI_OSS_CAPA;
344         }
345
346         sbi->ll_sdev_orig = sb->s_dev;
347 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
348         /* We set sb->s_dev equal on all lustre clients in order to support
349          * NFS export clustering.  NFSD requires that the FSID be the same
350          * on all clients. */
351         /* s_dev is also used in lt_compare() to compare two fs, but that is
352          * only a node-local comparison. */
353
354         /* XXX: this will not work with LMV */
355         sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_target_uuid.uuid,
356                                  strlen(sbi2mdc(sbi)->cl_target_uuid.uuid));
357 #endif
358
359         obd = class_name2obd(dt);
360         if (!obd) {
361                 CERROR("DT %s: not setup or attached\n", dt);
362                 GOTO(out_md_fid, err = -ENODEV);
363         }
364
365         data->ocd_connect_flags = OBD_CONNECT_GRANT     | OBD_CONNECT_VERSION  |
366                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
367                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID;
368         if (sbi->ll_flags & LL_SBI_OSS_CAPA)
369                 data->ocd_connect_flags |= OBD_CONNECT_OSS_CAPA;
370
371 #ifdef HAVE_LRU_RESIZE_SUPPORT
372         data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
373 #endif
374         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
375                "ocd_grant: %d\n", data->ocd_connect_flags,
376                data->ocd_version, data->ocd_grant);
377
378         obd->obd_upcall.onu_owner = &sbi->ll_lco;
379         obd->obd_upcall.onu_upcall = ll_ocd_update;
380         data->ocd_brw_size = PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT;
381
382         err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, data);
383         if (err == -EBUSY) {
384                 LCONSOLE_ERROR_MSG(0x150, "An OST (dt %s) is performing "
385                                    "recovery, of which this client is not a "
386                                    "part.  Please wait for recovery to "
387                                    "complete, abort, or time out.\n", dt);
388                 GOTO(out_md_fid, err);
389         } else if (err) {
390                 CERROR("Cannot connect to %s: rc = %d\n", dt, err);
391                 GOTO(out_md_fid, err);
392         }
393
394         sbi->ll_dt_exp = class_conn2export(&dt_conn);
395
396         err = obd_fid_init(sbi->ll_dt_exp);
397         if (err) {
398                 CERROR("Can't init data layer FID infrastructure, "
399                        "rc %d\n", err);
400                 GOTO(out_dt, err);
401         }
402         
403         spin_lock(&sbi->ll_lco.lco_lock);
404         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
405         spin_unlock(&sbi->ll_lco.lco_lock);
406
407         ll_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
408
409         err = obd_prep_async_page(sbi->ll_dt_exp, NULL, NULL, NULL,
410                                   0, NULL, NULL, NULL);
411         if (err < 0) {
412                 LCONSOLE_ERROR_MSG(0x151, "There are no OST's in this "
413                                    "filesystem. There must be at least one "
414                                    "active OST for a client to start.\n");
415                 GOTO(out_dt_fid, err);
416         }
417
418         if (!ll_async_page_slab) {
419                 ll_async_page_slab_size =
420                         size_round(sizeof(struct ll_async_page)) + err;
421                 ll_async_page_slab = cfs_mem_cache_create("ll_async_page",
422                                                           ll_async_page_slab_size,
423                                                           0, 0);
424                 if (!ll_async_page_slab)
425                         GOTO(out_dt_fid, err = -ENOMEM);
426         }
427
428         err = md_getstatus(sbi->ll_md_exp, &rootfid, &oc);
429         if (err) {
430                 CERROR("cannot mds_connect: rc = %d\n", err);
431                 GOTO(out_dt_fid, err);
432         }
433         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
434         sbi->ll_root_fid = rootfid;
435
436         sb->s_op = &lustre_super_operations;
437         sb->s_export_op = &lustre_export_operations;
438
439         /* make root inode
440          * XXX: move this to after cbd setup? */
441         valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS | OBD_MD_FLMDSCAPA;
442         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
443                 valid |= OBD_MD_FLRMTPERM;
444         else if (sbi->ll_flags & LL_SBI_ACL)
445                 valid |= OBD_MD_FLACL;
446
447         err = md_getattr(sbi->ll_md_exp, &rootfid, oc, valid, 0, &request);
448         if (oc)
449                 free_capa(oc);
450         if (err) {
451                 CERROR("md_getattr failed for root: rc = %d\n", err);
452                 GOTO(out_dt_fid, err);
453         }
454         memset(&lmd, 0, sizeof(lmd));
455         err = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
456                                sbi->ll_md_exp, &lmd);
457         if (err) {
458                 CERROR("failed to understand root inode md: rc = %d\n", err);
459                 ptlrpc_req_finished (request);
460                 GOTO(out_dt_fid, err);
461         }
462
463         LASSERT(fid_is_sane(&sbi->ll_root_fid));
464         root = ll_iget(sb, ll_fid_build_ino(sbi, &sbi->ll_root_fid), &lmd);
465         md_free_lustre_md(sbi->ll_md_exp, &lmd);
466         ptlrpc_req_finished(request);
467
468         if (root == NULL || is_bad_inode(root)) {
469                 if (lmd.lsm)
470                         obd_free_memmd(sbi->ll_dt_exp, &lmd.lsm);
471 #ifdef CONFIG_FS_POSIX_ACL
472                 if (lmd.posix_acl) {
473                         posix_acl_release(lmd.posix_acl);
474                         lmd.posix_acl = NULL;
475                 }
476 #endif
477                 CERROR("lustre_lite: bad iget4 for root\n");
478                 GOTO(out_root, err = -EBADF);
479         }
480
481         err = ll_close_thread_start(&sbi->ll_lcq);
482         if (err) {
483                 CERROR("cannot start close thread: rc %d\n", err);
484                 GOTO(out_root, err);
485         }
486
487 #ifdef CONFIG_FS_POSIX_ACL
488         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
489                 rct_init(&sbi->ll_rct);
490                 et_init(&sbi->ll_et);
491         }
492 #endif
493
494         checksum = sbi->ll_flags & LL_SBI_CHECKSUM;
495         err = obd_set_info_async(sbi->ll_dt_exp, strlen("checksum"),"checksum",
496                                  sizeof(checksum), &checksum, NULL);
497
498         sb->s_root = d_alloc_root(root);
499         if (data != NULL)
500                 OBD_FREE(data, sizeof(*data));
501         sb->s_root->d_op = &ll_d_root_ops;
502         RETURN(err);
503 out_root:
504         if (root)
505                 iput(root);
506 out_dt_fid:
507         obd_fid_fini(sbi->ll_dt_exp);
508 out_dt:
509         obd_disconnect(sbi->ll_dt_exp);
510         sbi->ll_dt_exp = NULL;
511 out_md_fid:
512         obd_fid_fini(sbi->ll_md_exp);
513 out_md:
514         obd_disconnect(sbi->ll_md_exp);
515         sbi->ll_md_exp = NULL;
516 out:
517         if (data != NULL)
518                 OBD_FREE_PTR(data);
519         lprocfs_unregister_mountpoint(sbi);
520         return err;
521 }
522
523 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
524 {
525         int size, rc;
526
527         *lmmsize = obd_size_diskmd(sbi->ll_dt_exp, NULL);
528         size = sizeof(int);
529         rc = obd_get_info(sbi->ll_md_exp, strlen("max_easize"), "max_easize",
530                           &size, lmmsize);
531         if (rc)
532                 CERROR("Get max mdsize error rc %d \n", rc);
533
534         RETURN(rc);
535 }
536
537 void ll_dump_inode(struct inode *inode)
538 {
539         struct list_head *tmp;
540         int dentry_count = 0;
541
542         LASSERT(inode != NULL);
543
544         list_for_each(tmp, &inode->i_dentry)
545                 dentry_count++;
546
547         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
548                inode, ll_i2mdexp(inode)->exp_obd->obd_name, inode->i_ino,
549                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
550 }
551
552 void lustre_dump_dentry(struct dentry *dentry, int recur)
553 {
554         struct list_head *tmp;
555         int subdirs = 0;
556
557         LASSERT(dentry != NULL);
558
559         list_for_each(tmp, &dentry->d_subdirs)
560                 subdirs++;
561
562         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
563                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
564                dentry->d_name.len, dentry->d_name.name,
565                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
566                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
567                dentry->d_flags, dentry->d_fsdata, subdirs);
568         if (dentry->d_inode != NULL)
569                 ll_dump_inode(dentry->d_inode);
570
571         if (recur == 0)
572                 return;
573
574         list_for_each(tmp, &dentry->d_subdirs) {
575                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
576                 lustre_dump_dentry(d, recur - 1);
577         }
578 }
579
580 #ifdef HAVE_EXPORT___IGET
581 static void prune_dir_dentries(struct inode *inode)
582 {
583         struct dentry *dentry, *prev = NULL;
584
585         /* due to lustre specific logic, a directory
586          * can have few dentries - a bug from VFS POV */
587 restart:
588         spin_lock(&dcache_lock);
589         if (!list_empty(&inode->i_dentry)) {
590                 dentry = list_entry(inode->i_dentry.prev,
591                                     struct dentry, d_alias);
592                 /* in order to prevent infinite loops we
593                  * break if previous dentry is busy */
594                 if (dentry != prev) {
595                         prev = dentry;
596                         dget_locked(dentry);
597                         spin_unlock(&dcache_lock);
598
599                         /* try to kill all child dentries */
600                         lock_dentry(dentry);
601                         shrink_dcache_parent(dentry);
602                         unlock_dentry(dentry);
603                         dput(dentry);
604
605                         /* now try to get rid of current dentry */
606                         d_prune_aliases(inode);
607                         goto restart;
608                 }
609         }
610         spin_unlock(&dcache_lock);
611 }
612
613 static void prune_deathrow_one(struct ll_inode_info *lli)
614 {
615         struct inode *inode = ll_info2i(lli);
616
617         /* first, try to drop any dentries - they hold a ref on the inode */
618         if (S_ISDIR(inode->i_mode))
619                 prune_dir_dentries(inode);
620         else
621                 d_prune_aliases(inode);
622
623
624         /* if somebody still uses it, leave it */
625         LASSERT(atomic_read(&inode->i_count) > 0);
626         if (atomic_read(&inode->i_count) > 1)
627                 goto out;
628
629         CDEBUG(D_INODE, "inode %lu/%u(%d) looks a good candidate for prune\n",
630                inode->i_ino,inode->i_generation, atomic_read(&inode->i_count));
631
632         /* seems nobody uses it anymore */
633         inode->i_nlink = 0;
634
635 out:
636         iput(inode);
637         return;
638 }
639
640 static void prune_deathrow(struct ll_sb_info *sbi, int try)
641 {
642         struct ll_inode_info *lli;
643         int empty;
644
645         do {
646                 if (need_resched() && try)
647                         break;
648
649                 if (try) {
650                         if (!spin_trylock(&sbi->ll_deathrow_lock))
651                                 break;
652                 } else {
653                         spin_lock(&sbi->ll_deathrow_lock);
654                 }
655
656                 empty = 1;
657                 lli = NULL;
658                 if (!list_empty(&sbi->ll_deathrow)) {
659                         lli = list_entry(sbi->ll_deathrow.next,
660                                          struct ll_inode_info,
661                                          lli_dead_list);
662                         list_del_init(&lli->lli_dead_list);
663                         if (!list_empty(&sbi->ll_deathrow))
664                                 empty = 0;
665                 }
666                 spin_unlock(&sbi->ll_deathrow_lock);
667
668                 if (lli)
669                         prune_deathrow_one(lli);
670
671         } while (empty == 0);
672 }
673 #else /* !HAVE_EXPORT___IGET */
674 #define prune_deathrow(sbi, try) do {} while (0)
675 #endif /* HAVE_EXPORT___IGET */
676
677 void client_common_put_super(struct super_block *sb)
678 {
679         struct ll_sb_info *sbi = ll_s2sbi(sb);
680         ENTRY;
681
682 #ifdef CONFIG_FS_POSIX_ACL
683         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
684                 et_fini(&sbi->ll_et);
685                 rct_fini(&sbi->ll_rct);
686         }
687 #endif
688
689         obd_cancel_unused(sbi->ll_dt_exp, NULL, 0, NULL);
690
691         ll_close_thread_shutdown(sbi->ll_lcq);
692
693         /* destroy inodes in deathrow */
694         prune_deathrow(sbi, 0);
695
696         list_del(&sbi->ll_conn_chain);
697
698         obd_fid_fini(sbi->ll_dt_exp);
699         obd_disconnect(sbi->ll_dt_exp);
700         sbi->ll_dt_exp = NULL;
701
702         lprocfs_unregister_mountpoint(sbi);
703
704         obd_fid_fini(sbi->ll_md_exp);
705         obd_disconnect(sbi->ll_md_exp);
706         sbi->ll_md_exp = NULL;
707
708         EXIT;
709 }
710
711 void ll_kill_super(struct super_block *sb)
712 {
713         struct ll_sb_info *sbi;
714
715         ENTRY;
716
717         /* not init sb ?*/
718         if (!(sb->s_flags & MS_ACTIVE))
719                 return;
720
721         sbi = ll_s2sbi(sb);
722         /* we need restore s_dev from changed for clustred NFS before put_super
723          * because new kernels have cached s_dev and change sb->s_dev in
724          * put_super not affected real removing devices */
725         if (sbi)
726                 sb->s_dev = sbi->ll_sdev_orig;
727         EXIT;
728 }
729
730 char *ll_read_opt(const char *opt, char *data)
731 {
732         char *value;
733         char *retval;
734         ENTRY;
735
736         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
737         if (strncmp(opt, data, strlen(opt)))
738                 RETURN(NULL);
739         if ((value = strchr(data, '=')) == NULL)
740                 RETURN(NULL);
741
742         value++;
743         OBD_ALLOC(retval, strlen(value) + 1);
744         if (!retval) {
745                 CERROR("out of memory!\n");
746                 RETURN(NULL);
747         }
748
749         memcpy(retval, value, strlen(value)+1);
750         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
751         RETURN(retval);
752 }
753
754 static inline int ll_set_opt(const char *opt, char *data, int fl)
755 {
756         if (strncmp(opt, data, strlen(opt)) != 0)
757                 return(0);
758         else
759                 return(fl);
760 }
761
762 /* non-client-specific mount options are parsed in lmd_parse */
763 static int ll_options(char *options, int *flags)
764 {
765         int tmp;
766         char *s1 = options, *s2;
767         ENTRY;
768
769         if (!options) 
770                 RETURN(0);
771
772         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
773
774         while (*s1) {
775                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
776                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
777                 if (tmp) {
778                         *flags |= tmp;
779                         goto next;
780                 }
781                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
782                 if (tmp) {
783                         *flags |= tmp;
784                         goto next;
785                 }
786                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
787                 if (tmp) {
788                         *flags |= tmp;
789                         goto next;
790                 }
791                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
792                 if (tmp) {
793                         *flags &= ~tmp;
794                         goto next;
795                 }
796                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
797                 if (tmp) {
798                         *flags |= tmp;
799                         goto next;
800                 }
801                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
802                 if (tmp) {
803                         *flags &= ~tmp;
804                         goto next;
805                 }
806                 tmp = ll_set_opt("acl", s1, LL_SBI_ACL);
807                 if (tmp) {
808                         /* Ignore deprecated mount option.  The client will
809                          * always try to mount with ACL support, whether this
810                          * is used depends on whether server supports it. */
811                         goto next;
812                 }
813                 tmp = ll_set_opt("noacl", s1, LL_SBI_ACL);
814                 if (tmp) {
815                         goto next;
816                 }
817                 tmp = ll_set_opt("remote_client", s1, LL_SBI_RMT_CLIENT);
818                 if (tmp) {
819                         *flags |= tmp;
820                         goto next;
821                 }
822
823                 tmp = ll_set_opt("checksum", s1, LL_SBI_CHECKSUM);
824                 if (tmp) {
825                         *flags |= tmp;
826                         goto next;
827                 }
828                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_CHECKSUM);
829                 if (tmp) {
830                         *flags &= ~tmp;
831                         goto next;
832                 }
833                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
834                 if (tmp) {
835                         *flags |= tmp;
836                         goto next;
837                 }
838                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
839                 if (tmp) {
840                         *flags &= ~tmp;
841                         goto next;
842                 }
843
844                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
845                                    s1);
846                 RETURN(-EINVAL);
847
848 next:
849                 /* Find next opt */
850                 s2 = strchr(s1, ',');
851                 if (s2 == NULL)
852                         break;
853                 s1 = s2 + 1;
854         }
855         RETURN(0);
856 }
857
858 void ll_lli_init(struct ll_inode_info *lli)
859 {
860         lli->lli_inode_magic = LLI_INODE_MAGIC;
861         sema_init(&lli->lli_size_sem, 1);
862         sema_init(&lli->lli_write_sem, 1);
863         lli->lli_flags = 0;
864         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
865         spin_lock_init(&lli->lli_lock);
866         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
867         INIT_LIST_HEAD(&lli->lli_close_list);
868         lli->lli_inode_magic = LLI_INODE_MAGIC;
869         sema_init(&lli->lli_och_sem, 1);
870         lli->lli_mds_read_och = lli->lli_mds_write_och = NULL;
871         lli->lli_mds_exec_och = NULL;
872         lli->lli_open_fd_read_count = lli->lli_open_fd_write_count = 0;
873         lli->lli_open_fd_exec_count = 0;
874         INIT_LIST_HEAD(&lli->lli_dead_list);
875         lli->lli_remote_perms = NULL;
876         lli->lli_rmtperm_utime = 0;
877         sema_init(&lli->lli_rmtperm_sem, 1);
878         INIT_LIST_HEAD(&lli->lli_oss_capas);
879 }
880
881 int ll_fill_super(struct super_block *sb)
882 {
883         struct lustre_profile *lprof;
884         struct lustre_sb_info *lsi = s2lsi(sb);
885         struct ll_sb_info *sbi;
886         char  *dt = NULL, *md = NULL;
887         char  *profilenm = get_profile_name(sb);
888         struct config_llog_instance cfg = {0, };
889         char   ll_instance[sizeof(sb) * 2 + 1];
890         int    err;
891         ENTRY;
892
893         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
894
895         cfs_module_get();
896
897         sb->s_type->fs_flags |= FS_ODD_RENAME;
898         /* client additional sb info */
899         lsi->lsi_llsbi = sbi = ll_init_sbi();
900         if (!sbi) {
901                 cfs_module_put();
902                 RETURN(-ENOMEM);
903         }
904
905         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
906         if (err) 
907                 GOTO(out_free, err);
908
909         /* Generate a string unique to this super, in case some joker tries
910            to mount the same fs at two mount points.
911            Use the address of the super itself.*/
912         sprintf(ll_instance, "%p", sb);
913         cfg.cfg_instance = ll_instance;
914         cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
915
916         /* set up client obds */
917         err = lustre_process_log(sb, profilenm, &cfg);
918         if (err < 0) {
919                 CERROR("Unable to process log: %d\n", err);
920                 GOTO(out_free, err);
921         }
922
923         lprof = class_get_profile(profilenm);
924         if (lprof == NULL) {
925                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
926                                    " read from the MGS.  Does that filesystem "
927                                    "exist?\n", profilenm);
928                 GOTO(out_free, err = -EINVAL);
929         }
930         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
931                lprof->lp_md, lprof->lp_dt);
932
933         OBD_ALLOC(dt, strlen(lprof->lp_dt) +
934                   strlen(ll_instance) + 2);
935         if (!dt)
936                 GOTO(out_free, err = -ENOMEM);
937         sprintf(dt, "%s-%s", lprof->lp_dt, ll_instance);
938
939         OBD_ALLOC(md, strlen(lprof->lp_md) +
940                   strlen(ll_instance) + 2);
941         if (!md)
942                 GOTO(out_free, err = -ENOMEM);
943         sprintf(md, "%s-%s", lprof->lp_md, ll_instance);
944
945         /* connections, registrations, sb setup */
946         err = client_common_fill_super(sb, md, dt);
947
948 out_free:
949         if (md)
950                 OBD_FREE(md, strlen(md) + 1);
951         if (dt)
952                 OBD_FREE(dt, strlen(dt) + 1);
953         if (err) 
954                 ll_put_super(sb);
955         else
956                 LCONSOLE_WARN("Client %s has started\n", profilenm);        
957
958         RETURN(err);
959 } /* ll_fill_super */
960
961
962 void ll_put_super(struct super_block *sb)
963 {
964         struct config_llog_instance cfg;
965         char   ll_instance[sizeof(sb) * 2 + 1];
966         struct obd_device *obd;
967         struct lustre_sb_info *lsi = s2lsi(sb);
968         struct ll_sb_info *sbi = ll_s2sbi(sb);
969         char *profilenm = get_profile_name(sb);
970         int force = 1, next;
971         ENTRY;
972
973         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
974
975         ll_print_capa_stat(sbi);
976
977         sprintf(ll_instance, "%p", sb);
978         cfg.cfg_instance = ll_instance;
979         lustre_end_log(sb, NULL, &cfg);
980         
981         if (sbi->ll_md_exp) {
982                 obd = class_exp2obd(sbi->ll_md_exp);
983                 if (obd) 
984                         force = obd->obd_force;
985         }
986         
987         /* We need to set force before the lov_disconnect in 
988            lustre_common_put_super, since l_d cleans up osc's as well. */
989         if (force) {
990                 next = 0;
991                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
992                                                      &next)) != NULL) {
993                         obd->obd_force = force;
994                 }
995         }                       
996
997         if (sbi->ll_lcq) {
998                 /* Only if client_common_fill_super succeeded */
999                 client_common_put_super(sb);
1000         }
1001         next = 0;
1002         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1003                 class_manual_cleanup(obd);
1004         }
1005
1006         if (profilenm)
1007                 class_del_profile(profilenm);
1008
1009         ll_free_sbi(sb);
1010         lsi->lsi_llsbi = NULL;
1011
1012         lustre_common_put_super(sb);
1013
1014         LCONSOLE_WARN("client %s umount complete\n", ll_instance);
1015         
1016         cfs_module_put();
1017
1018         EXIT;
1019 } /* client_put_super */
1020
1021 #ifdef HAVE_REGISTER_CACHE
1022 #include <linux/cache_def.h>
1023 #ifdef HAVE_CACHE_RETURN_INT
1024 static int
1025 #else
1026 static void
1027 #endif
1028 ll_shrink_cache(int priority, unsigned int gfp_mask)
1029 {
1030         struct ll_sb_info *sbi;
1031         int count = 0;
1032
1033         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
1034                 count += llap_shrink_cache(sbi, priority);
1035
1036 #ifdef HAVE_CACHE_RETURN_INT
1037         return count;
1038 #endif
1039 }
1040
1041 struct cache_definition ll_cache_definition = {
1042         .name = "llap_cache",
1043         .shrink = ll_shrink_cache
1044 };
1045 #endif /* HAVE_REGISTER_CACHE */
1046
1047 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1048 {
1049         struct inode *inode = NULL;
1050         /* NOTE: we depend on atomic igrab() -bzzz */
1051         lock_res_and_lock(lock);
1052         if (lock->l_ast_data) {
1053                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1054                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1055                         inode = igrab(lock->l_ast_data);
1056                 } else {
1057                         inode = lock->l_ast_data;
1058                         ldlm_lock_debug(NULL, inode->i_state & I_FREEING ?
1059                                                 D_INFO : D_WARNING,
1060                                         lock, __FILE__, __func__, __LINE__,
1061                                         "l_ast_data %p is bogus: magic %08x",
1062                                         lock->l_ast_data, lli->lli_inode_magic);
1063                         inode = NULL;
1064                 }
1065         }
1066         unlock_res_and_lock(lock);
1067         return inode;
1068 }
1069
1070 static int null_if_equal(struct ldlm_lock *lock, void *data)
1071 {
1072         if (data == lock->l_ast_data) {
1073                 lock->l_ast_data = NULL;
1074
1075                 if (lock->l_req_mode != lock->l_granted_mode)
1076                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
1077         }
1078
1079         return LDLM_ITER_CONTINUE;
1080 }
1081
1082 void ll_clear_inode(struct inode *inode)
1083 {
1084         struct ll_inode_info *lli = ll_i2info(inode);
1085         struct ll_sb_info *sbi = ll_i2sbi(inode);
1086         ENTRY;
1087
1088         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1089                inode->i_generation, inode);
1090
1091         ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
1092         md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
1093                          null_if_equal, inode);
1094
1095         LASSERT(!lli->lli_open_fd_write_count);
1096         LASSERT(!lli->lli_open_fd_read_count);
1097         LASSERT(!lli->lli_open_fd_exec_count);
1098
1099         if (lli->lli_mds_write_och)
1100                 ll_md_real_close(inode, FMODE_WRITE);
1101         if (lli->lli_mds_exec_och)
1102                 ll_md_real_close(inode, FMODE_EXEC);
1103         if (lli->lli_mds_read_och)
1104                 ll_md_real_close(inode, FMODE_READ);
1105
1106         if (lli->lli_smd) {
1107                 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
1108                                   null_if_equal, inode);
1109
1110                 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
1111                 lli->lli_smd = NULL;
1112         }
1113
1114         if (lli->lli_symlink_name) {
1115                 OBD_FREE(lli->lli_symlink_name,
1116                          strlen(lli->lli_symlink_name) + 1);
1117                 lli->lli_symlink_name = NULL;
1118         }
1119
1120         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1121                 LASSERT(lli->lli_posix_acl == NULL);
1122                 if (lli->lli_remote_perms) {
1123                         free_rmtperm_hash(lli->lli_remote_perms);
1124                         lli->lli_remote_perms = NULL;
1125                 }
1126         }
1127 #ifdef CONFIG_FS_POSIX_ACL
1128         else if (lli->lli_posix_acl) {
1129                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1130                 LASSERT(lli->lli_remote_perms == NULL);
1131                 posix_acl_release(lli->lli_posix_acl);
1132                 lli->lli_posix_acl = NULL;
1133         }
1134 #endif
1135         lli->lli_inode_magic = LLI_INODE_DEAD;
1136
1137 #ifdef HAVE_EXPORT___IGET
1138         spin_lock(&sbi->ll_deathrow_lock);
1139         list_del_init(&lli->lli_dead_list);
1140         spin_unlock(&sbi->ll_deathrow_lock);
1141 #endif
1142         ll_clear_inode_capas(inode);
1143
1144         EXIT;
1145 }
1146
1147 int ll_md_setattr(struct inode *inode, struct md_op_data *op_data,
1148                   struct md_open_data **mod)
1149 {
1150         struct lustre_md md;
1151         struct ll_sb_info *sbi = ll_i2sbi(inode);
1152         struct ptlrpc_request *request = NULL;
1153         int rc;
1154         ENTRY;
1155         
1156         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, 
1157                                      LUSTRE_OPC_ANY, NULL);
1158         if (IS_ERR(op_data))
1159                 RETURN(PTR_ERR(op_data));
1160
1161         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL, 0, 
1162                         &request, mod);
1163         if (rc) {
1164                 ptlrpc_req_finished(request);
1165                 if (rc == -ENOENT) {
1166                         inode->i_nlink = 0;
1167                         /* Unlinked special device node? Or just a race?
1168                          * Pretend we done everything. */
1169                         if (!S_ISREG(inode->i_mode) &&
1170                             !S_ISDIR(inode->i_mode))
1171                                 rc = inode_setattr(inode, &op_data->op_attr);
1172                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1173                         CERROR("md_setattr fails: rc = %d\n", rc);
1174                 }
1175                 RETURN(rc);
1176         }
1177
1178         rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1179                               sbi->ll_md_exp, &md);
1180         if (rc) {
1181                 ptlrpc_req_finished(request);
1182                 RETURN(rc);
1183         }
1184
1185         /* We call inode_setattr to adjust timestamps.
1186          * If there is at least some data in file, we cleared ATTR_SIZE
1187          * above to avoid invoking vmtruncate, otherwise it is important
1188          * to call vmtruncate in inode_setattr to update inode->i_size
1189          * (bug 6196) */
1190         rc = inode_setattr(inode, &op_data->op_attr);
1191
1192         /* Extract epoch data if obtained. */
1193         op_data->op_handle = md.body->handle;
1194         op_data->op_ioepoch = md.body->ioepoch;
1195
1196         ll_update_inode(inode, &md);
1197         ptlrpc_req_finished(request);
1198
1199         RETURN(rc);
1200 }
1201
1202 /* Close IO epoch and send Size-on-MDS attribute update. */
1203 static int ll_setattr_done_writing(struct inode *inode,
1204                                    struct md_op_data *op_data,
1205                                    struct md_open_data *mod)
1206 {
1207         struct ll_inode_info *lli = ll_i2info(inode);
1208         int rc = 0;
1209         ENTRY;
1210         
1211         LASSERT(op_data != NULL);
1212         if (!S_ISREG(inode->i_mode))
1213                 RETURN(0);
1214
1215         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
1216                op_data->op_ioepoch, PFID(&lli->lli_fid));
1217
1218         op_data->op_flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
1219         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, mod);
1220         if (rc == -EAGAIN) {
1221                 /* MDS has instructed us to obtain Size-on-MDS attribute
1222                  * from OSTs and send setattr to back to MDS. */
1223                 rc = ll_sizeonmds_update(inode, mod, &op_data->op_handle,
1224                                          op_data->op_ioepoch);
1225         } else if (rc) {
1226                 CERROR("inode %lu mdc truncate failed: rc = %d\n",
1227                        inode->i_ino, rc);
1228         }
1229         RETURN(rc);
1230 }
1231
1232 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1233  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1234  * keep these values until such a time that objects are allocated for it.
1235  * We do the MDS operations first, as it is checking permissions for us.
1236  * We don't to the MDS RPC if there is nothing that we want to store there,
1237  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1238  * going to do an RPC anyways.
1239  *
1240  * If we are doing a truncate, we will send the mtime and ctime updates
1241  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1242  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1243  * at the same time.
1244  */
1245 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1246 {
1247         struct ll_inode_info *lli = ll_i2info(inode);
1248         struct lov_stripe_md *lsm = lli->lli_smd;
1249         struct ll_sb_info *sbi = ll_i2sbi(inode);
1250         struct md_op_data *op_data = NULL;
1251         struct md_open_data *mod = NULL;
1252         int ia_valid = attr->ia_valid;
1253         int rc = 0, rc1 = 0;
1254         ENTRY;
1255
1256         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
1257                attr->ia_valid);
1258         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETATTR, 1);
1259
1260         if (ia_valid & ATTR_SIZE) {
1261                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1262                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1263                                attr->ia_size, ll_file_maxbytes(inode));
1264                         RETURN(-EFBIG);
1265                 }
1266
1267                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1268         }
1269
1270         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1271         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1272                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1273                         RETURN(-EPERM);
1274         }
1275
1276         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1277         if (attr->ia_valid & ATTR_CTIME) {
1278                 attr->ia_ctime = CURRENT_TIME;
1279                 attr->ia_valid |= ATTR_CTIME_SET;
1280         }
1281         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1282                 attr->ia_atime = CURRENT_TIME;
1283                 attr->ia_valid |= ATTR_ATIME_SET;
1284         }
1285         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1286                 attr->ia_mtime = CURRENT_TIME;
1287                 attr->ia_valid |= ATTR_MTIME_SET;
1288         }
1289         if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
1290                 /* To avoid stale mtime on mds, obtain it from ost and send 
1291                    to mds. */
1292                 rc = ll_glimpse_size(inode, 0);
1293                 if (rc) 
1294                         RETURN(rc);
1295                 
1296                 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
1297                 attr->ia_mtime = inode->i_mtime;
1298         }
1299
1300         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1301                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1302                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1303                        CURRENT_SECONDS);
1304
1305         /* NB: ATTR_SIZE will only be set after this point if the size
1306          * resides on the MDS, ie, this file has no objects. */
1307         if (lsm)
1308                 attr->ia_valid &= ~ATTR_SIZE;
1309
1310         /* We always do an MDS RPC, even if we're only changing the size;
1311          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1312
1313         OBD_ALLOC_PTR(op_data);
1314         if (op_data == NULL)
1315                 RETURN(-ENOMEM);
1316         
1317         memcpy(&op_data->op_attr, attr, sizeof(*attr));
1318
1319         /* Open epoch for truncate. */
1320         if (ia_valid & ATTR_SIZE)
1321                 op_data->op_flags = MF_EPOCH_OPEN;
1322         
1323         rc = ll_md_setattr(inode, op_data, &mod);
1324         if (rc)
1325                 GOTO(out, rc);
1326
1327         if (op_data->op_ioepoch)
1328                 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for "
1329                        "truncate\n", op_data->op_ioepoch, PFID(&lli->lli_fid));
1330
1331         if (!lsm || !S_ISREG(inode->i_mode)) {
1332                 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1333                 GOTO(out, rc = 0);
1334         }
1335
1336         /* We really need to get our PW lock before we change inode->i_size.
1337          * If we don't we can race with other i_size updaters on our node, like
1338          * ll_file_read.  We can also race with i_size propogation to other
1339          * nodes through dirtying and writeback of final cached pages.  This
1340          * last one is especially bad for racing o_append users on other
1341          * nodes. */
1342         if (ia_valid & ATTR_SIZE) {
1343                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1344                                                            OBD_OBJECT_EOF } };
1345                 struct lustre_handle lockh = { 0 };
1346                 int err, ast_flags = 0;
1347                 /* XXX when we fix the AST intents to pass the discard-range
1348                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1349                  * XXX here. */
1350                 if (attr->ia_size == 0)
1351                         ast_flags = LDLM_AST_DISCARD_DATA;
1352
1353                 UNLOCK_INODE_MUTEX(inode);
1354                 UP_WRITE_I_ALLOC_SEM(inode);
1355                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1356                                     ast_flags);
1357                 LOCK_INODE_MUTEX(inode);
1358                 DOWN_WRITE_I_ALLOC_SEM(inode);
1359
1360                 if (rc != 0)
1361                         GOTO(out, rc);
1362
1363                 /* Only ll_inode_size_lock is taken at this level.
1364                  * lov_stripe_lock() is grabbed by ll_truncate() only over
1365                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
1366                  * ll_truncate dropped ll_inode_size_lock() */
1367                 ll_inode_size_lock(inode, 0);
1368                 rc = vmtruncate(inode, attr->ia_size);
1369                 if (rc != 0) {
1370                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1371                         ll_inode_size_unlock(inode, 0);
1372                 }
1373
1374                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1375                 if (err) {
1376                         CERROR("ll_extent_unlock failed: %d\n", err);
1377                         if (!rc)
1378                                 rc = err;
1379                 }
1380         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1381                 obd_flag flags;
1382                 struct obd_info oinfo = { { { 0 } } };
1383                 struct obdo *oa;
1384
1385                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1386                        inode->i_ino, LTIME_S(attr->ia_mtime));
1387
1388                 OBDO_ALLOC(oa);
1389                 if (oa) {
1390                         oa->o_id = lsm->lsm_object_id;
1391                         oa->o_gr = lsm->lsm_object_gr;
1392                         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1393
1394                         flags = OBD_MD_FLTYPE | OBD_MD_FLATIME |
1395                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1396                                 OBD_MD_FLFID | OBD_MD_FLGENER | 
1397                                 OBD_MD_FLGROUP;
1398
1399                         obdo_from_inode(oa, inode, flags);
1400
1401                         oinfo.oi_oa = oa;
1402                         oinfo.oi_md = lsm;
1403                         oinfo.oi_capa = ll_mdscapa_get(inode);
1404
1405                         /* XXX: this looks unnecessary now. */
1406                         rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
1407                         capa_put(oinfo.oi_capa);
1408                         if (rc)
1409                                 CERROR("obd_setattr_async fails: rc=%d\n", rc);
1410                         OBDO_FREE(oa);
1411                 } else {
1412                         rc = -ENOMEM;
1413                 }
1414         }
1415         EXIT;
1416 out:
1417         if (op_data) {
1418                 if (op_data->op_ioepoch)
1419                         rc1 = ll_setattr_done_writing(inode, op_data, mod);
1420                 ll_finish_md_op_data(op_data);
1421         }
1422         return rc ? rc : rc1;
1423 }
1424
1425 int ll_setattr(struct dentry *de, struct iattr *attr)
1426 {
1427         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1428             (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1429                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1430
1431         return ll_setattr_raw(de->d_inode, attr);
1432 }
1433
1434 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1435                        __u64 max_age)
1436 {
1437         struct ll_sb_info *sbi = ll_s2sbi(sb);
1438         struct obd_statfs obd_osfs;
1439         int rc;
1440         ENTRY;
1441
1442         rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1443         if (rc) {
1444                 CERROR("md_statfs fails: rc = %d\n", rc);
1445                 RETURN(rc);
1446         }
1447
1448         osfs->os_type = sb->s_magic;
1449
1450         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1451                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1452
1453         rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1454                               &obd_osfs, max_age, 0);
1455         if (rc) {
1456                 CERROR("obd_statfs fails: rc = %d\n", rc);
1457                 RETURN(rc);
1458         }
1459
1460         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1461                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1462                obd_osfs.os_files);
1463
1464         osfs->os_bsize = obd_osfs.os_bsize;
1465         osfs->os_blocks = obd_osfs.os_blocks;
1466         osfs->os_bfree = obd_osfs.os_bfree;
1467         osfs->os_bavail = obd_osfs.os_bavail;
1468
1469         /* If we don't have as many objects free on the OST as inodes
1470          * on the MDS, we reduce the total number of inodes to
1471          * compensate, so that the "inodes in use" number is correct.
1472          */
1473         if (obd_osfs.os_ffree < osfs->os_ffree) {
1474                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1475                         obd_osfs.os_ffree;
1476                 osfs->os_ffree = obd_osfs.os_ffree;
1477         }
1478
1479         RETURN(rc);
1480 }
1481 #ifndef HAVE_STATFS_DENTRY_PARAM
1482 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1483 {
1484 #else
1485 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1486 {
1487         struct super_block *sb = de->d_sb;
1488 #endif
1489         struct obd_statfs osfs;
1490         int rc;
1491
1492         CDEBUG(D_VFSTRACE, "VFS Op: at "LPU64" jiffies\n", get_jiffies_64());
1493         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1494
1495         /* For now we will always get up-to-date statfs values, but in the
1496          * future we may allow some amount of caching on the client (e.g.
1497          * from QOS or lprocfs updates). */
1498         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - 1);
1499         if (rc)
1500                 return rc;
1501
1502         statfs_unpack(sfs, &osfs);
1503
1504         /* We need to downshift for all 32-bit kernels, because we can't
1505          * tell if the kernel is being called via sys_statfs64() or not.
1506          * Stop before overflowing f_bsize - in which case it is better
1507          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1508         if (sizeof(long) < 8) {
1509                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1510                         sfs->f_bsize <<= 1;
1511
1512                         osfs.os_blocks >>= 1;
1513                         osfs.os_bfree >>= 1;
1514                         osfs.os_bavail >>= 1;
1515                 }
1516         }
1517
1518         sfs->f_blocks = osfs.os_blocks;
1519         sfs->f_bfree = osfs.os_bfree;
1520         sfs->f_bavail = osfs.os_bavail;
1521
1522         return 0;
1523 }
1524
1525 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
1526 {
1527         struct ll_inode_info *lli;
1528         struct lov_stripe_md *lsm;
1529
1530         lli = ll_i2info(inode);
1531         LASSERT(lli->lli_size_sem_owner != current);
1532         down(&lli->lli_size_sem);
1533         LASSERT(lli->lli_size_sem_owner == NULL);
1534         lli->lli_size_sem_owner = current;
1535         lsm = lli->lli_smd;
1536         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
1537                  lsm, lock_lsm);
1538         if (lock_lsm)
1539                 lov_stripe_lock(lsm);
1540 }
1541
1542 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1543 {
1544         struct ll_inode_info *lli;
1545         struct lov_stripe_md *lsm;
1546
1547         lli = ll_i2info(inode);
1548         lsm = lli->lli_smd;
1549         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1550                  lsm, unlock_lsm);
1551         if (unlock_lsm)
1552                 lov_stripe_unlock(lsm);
1553         LASSERT(lli->lli_size_sem_owner == current);
1554         lli->lli_size_sem_owner = NULL;
1555         up(&lli->lli_size_sem);
1556 }
1557
1558 static void ll_replace_lsm(struct inode *inode, struct lov_stripe_md *lsm)
1559 {
1560         struct ll_inode_info *lli = ll_i2info(inode);
1561
1562         dump_lsm(D_INODE, lsm);
1563         dump_lsm(D_INODE, lli->lli_smd);
1564         LASSERTF(lsm->lsm_magic == LOV_MAGIC_JOIN,
1565                  "lsm must be joined lsm %p\n", lsm);
1566         obd_free_memmd(ll_i2dtexp(inode), &lli->lli_smd);
1567         CDEBUG(D_INODE, "replace lsm %p to lli_smd %p for inode %lu%u(%p)\n",
1568                lsm, lli->lli_smd, inode->i_ino, inode->i_generation, inode);
1569         lli->lli_smd = lsm;
1570         lli->lli_maxbytes = lsm->lsm_maxbytes;
1571         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1572                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1573 }
1574
1575 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1576 {
1577         struct ll_inode_info *lli = ll_i2info(inode);
1578         struct mdt_body *body = md->body;
1579         struct lov_stripe_md *lsm = md->lsm;
1580         struct ll_sb_info *sbi = ll_i2sbi(inode);
1581
1582         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1583         if (lsm != NULL) {
1584                 if (lli->lli_smd == NULL) {
1585                         if (lsm->lsm_magic != LOV_MAGIC &&
1586                             lsm->lsm_magic != LOV_MAGIC_JOIN) {
1587                                 dump_lsm(D_ERROR, lsm);
1588                                 LBUG();
1589                         }
1590                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1591                                lsm, inode->i_ino, inode->i_generation, inode);
1592                         /* ll_inode_size_lock() requires it is only called
1593                          * with lli_smd != NULL or lock_lsm == 0 or we can
1594                          * race between lock/unlock.  bug 9547 */
1595                         lli->lli_smd = lsm;
1596                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1597                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1598                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1599                 } else {
1600                         if (lli->lli_smd->lsm_magic == lsm->lsm_magic &&
1601                              lli->lli_smd->lsm_stripe_count ==
1602                                         lsm->lsm_stripe_count) {
1603                                 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1604                                         CERROR("lsm mismatch for inode %ld\n",
1605                                                 inode->i_ino);
1606                                         CERROR("lli_smd:\n");
1607                                         dump_lsm(D_ERROR, lli->lli_smd);
1608                                         CERROR("lsm:\n");
1609                                         dump_lsm(D_ERROR, lsm);
1610                                         LBUG();
1611                                 }
1612                         } else
1613                                 ll_replace_lsm(inode, lsm);
1614                 }
1615                 if (lli->lli_smd != lsm)
1616                         obd_free_memmd(ll_i2dtexp(inode), &lsm);
1617         }
1618
1619         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1620                 if (body->valid & OBD_MD_FLRMTPERM)
1621                         ll_update_remote_perm(inode, md->remote_perm);
1622         }
1623 #ifdef CONFIG_FS_POSIX_ACL
1624         else if (body->valid & OBD_MD_FLACL) {
1625                 spin_lock(&lli->lli_lock);
1626                 if (lli->lli_posix_acl)
1627                         posix_acl_release(lli->lli_posix_acl);
1628                 lli->lli_posix_acl = md->posix_acl;
1629                 spin_unlock(&lli->lli_lock);
1630         }
1631 #endif
1632         if (body->valid & OBD_MD_FLATIME &&
1633             body->atime > LTIME_S(inode->i_atime))
1634                 LTIME_S(inode->i_atime) = body->atime;
1635         
1636         /* mtime is always updated with ctime, but can be set in past.
1637            As write and utime(2) may happen within 1 second, and utime's
1638            mtime has a priority over write's one, so take mtime from mds 
1639            for the same ctimes. */
1640         if (body->valid & OBD_MD_FLCTIME &&
1641             body->ctime >= LTIME_S(inode->i_ctime)) {
1642                 LTIME_S(inode->i_ctime) = body->ctime;
1643                 if (body->valid & OBD_MD_FLMTIME) {
1644                         CDEBUG(D_INODE, "setting ino %lu mtime "
1645                                "from %lu to "LPU64"\n", inode->i_ino, 
1646                                LTIME_S(inode->i_mtime), body->mtime);
1647                         LTIME_S(inode->i_mtime) = body->mtime;
1648                 }
1649         }
1650         if (body->valid & OBD_MD_FLMODE)
1651                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1652         if (body->valid & OBD_MD_FLTYPE)
1653                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1654         if (S_ISREG(inode->i_mode)) {
1655                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1, LL_MAX_BLKSIZE_BITS);
1656         } else {
1657                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1658         }
1659 #ifdef HAVE_INODE_BLKSIZE
1660         inode->i_blksize = 1<<inode->i_blkbits;
1661 #endif
1662         if (body->valid & OBD_MD_FLUID)
1663                 inode->i_uid = body->uid;
1664         if (body->valid & OBD_MD_FLGID)
1665                 inode->i_gid = body->gid;
1666         if (body->valid & OBD_MD_FLFLAGS)
1667                 inode->i_flags = ll_ext_to_inode_flags(body->flags);
1668         if (body->valid & OBD_MD_FLNLINK)
1669                 inode->i_nlink = body->nlink;
1670         if (body->valid & OBD_MD_FLRDEV)
1671                 inode->i_rdev = old_decode_dev(body->rdev);
1672
1673         if (body->valid & OBD_MD_FLID) {
1674                 /* FID shouldn't be changed! */
1675                 if (fid_is_sane(&lli->lli_fid)) {
1676                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->fid1),
1677                                  "Trying to change FID "DFID
1678                                  " to the "DFID", inode %lu/%u(%p)\n",
1679                                  PFID(&lli->lli_fid), PFID(&body->fid1),
1680                                  inode->i_ino, inode->i_generation, inode);
1681                 } else 
1682                         lli->lli_fid = body->fid1;
1683         }
1684
1685         LASSERT(fid_seq(&lli->lli_fid) != 0);
1686
1687         if (body->valid & OBD_MD_FLSIZE) {
1688                 if ((ll_i2mdexp(inode)->exp_connect_flags & OBD_CONNECT_SOM) &&
1689                     S_ISREG(inode->i_mode) && lli->lli_smd) {
1690                         struct lustre_handle lockh;
1691                         ldlm_mode_t mode;
1692                         
1693                         /* As it is possible a blocking ast has been processed
1694                          * by this time, we need to check there is an UPDATE 
1695                          * lock on the client and set LLIF_MDS_SIZE_LOCK holding
1696                          * it. */
1697                         mode = ll_take_md_lock(inode, MDS_INODELOCK_UPDATE,
1698                                                &lockh);
1699                         if (mode) {
1700                                 if (lli->lli_flags & (LLIF_DONE_WRITING |
1701                                                       LLIF_EPOCH_PENDING |
1702                                                       LLIF_SOM_DIRTY)) {
1703                                         CERROR("ino %lu flags %lu still has "
1704                                                "size authority! do not trust "
1705                                                "the size got from MDS\n",
1706                                                inode->i_ino, lli->lli_flags);
1707                                 } else {
1708                                         /* Use old size assignment to avoid
1709                                          * deadlock bz14138 & bz14326 */
1710                                         inode->i_size = body->size;
1711                                         lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
1712                                 }
1713                                 ldlm_lock_decref(&lockh, mode);
1714                         }
1715                 } else {
1716                         /* Use old size assignment to avoid
1717                          * deadlock bz14138 & bz14326 */
1718                         inode->i_size = body->size;
1719                 }
1720
1721                 if (body->valid & OBD_MD_FLBLOCKS)
1722                         inode->i_blocks = body->blocks;
1723         }
1724
1725         if (body->valid & OBD_MD_FLMDSCAPA) {
1726                 LASSERT(md->mds_capa);
1727                 ll_add_capa(inode, md->mds_capa);
1728         }
1729         if (body->valid & OBD_MD_FLOSSCAPA) {
1730                 LASSERT(md->oss_capa);
1731                 ll_add_capa(inode, md->oss_capa);
1732         }
1733 }
1734
1735 static struct backing_dev_info ll_backing_dev_info = {
1736         .ra_pages       = 0,    /* No readahead */
1737 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
1738         .capabilities   = 0,    /* Does contribute to dirty memory */
1739 #else
1740         .memory_backed  = 0,    /* Does contribute to dirty memory */
1741 #endif
1742 };
1743
1744 void ll_read_inode2(struct inode *inode, void *opaque)
1745 {
1746         struct lustre_md *md = opaque;
1747         struct ll_inode_info *lli = ll_i2info(inode);
1748         ENTRY;
1749
1750         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
1751                inode->i_ino, inode->i_generation, inode);
1752
1753         ll_lli_init(lli);
1754
1755         LASSERT(!lli->lli_smd);
1756
1757         /* Core attributes from the MDS first.  This is a new inode, and
1758          * the VFS doesn't zero times in the core inode so we have to do
1759          * it ourselves.  They will be overwritten by either MDS or OST
1760          * attributes - we just need to make sure they aren't newer. */
1761         LTIME_S(inode->i_mtime) = 0;
1762         LTIME_S(inode->i_atime) = 0;
1763         LTIME_S(inode->i_ctime) = 0;
1764         inode->i_rdev = 0;
1765         ll_update_inode(inode, md);
1766
1767         /* OIDEBUG(inode); */
1768
1769         if (S_ISREG(inode->i_mode)) {
1770                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1771                 inode->i_op = &ll_file_inode_operations;
1772                 inode->i_fop = sbi->ll_fop;
1773                 inode->i_mapping->a_ops = &ll_aops;
1774                 EXIT;
1775         } else if (S_ISDIR(inode->i_mode)) {
1776                 inode->i_op = &ll_dir_inode_operations;
1777                 inode->i_fop = &ll_dir_operations;
1778                 inode->i_mapping->a_ops = &ll_dir_aops;
1779                 EXIT;
1780         } else if (S_ISLNK(inode->i_mode)) {
1781                 inode->i_op = &ll_fast_symlink_inode_operations;
1782                 EXIT;
1783         } else {
1784                 inode->i_op = &ll_special_inode_operations;
1785
1786                 init_special_inode(inode, inode->i_mode,
1787                                    kdev_t_to_nr(inode->i_rdev));
1788
1789                 /* initializing backing dev info. */
1790                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1791
1792                 EXIT;
1793         }
1794 }
1795
1796 void ll_delete_inode(struct inode *inode)
1797 {
1798         struct ll_sb_info *sbi = ll_i2sbi(inode);
1799         int rc;
1800         ENTRY;
1801
1802         rc = obd_fid_delete(sbi->ll_md_exp, ll_inode2fid(inode));
1803         if (rc) {
1804                 CERROR("fid_delete() failed, rc %d\n", rc);
1805         }
1806         clear_inode(inode);
1807
1808         EXIT;
1809 }
1810
1811 int ll_iocontrol(struct inode *inode, struct file *file,
1812                  unsigned int cmd, unsigned long arg)
1813 {
1814         struct ll_sb_info *sbi = ll_i2sbi(inode);
1815         struct ptlrpc_request *req = NULL;
1816         int rc, flags = 0;
1817         ENTRY;
1818
1819         switch(cmd) {
1820         case EXT3_IOC_GETFLAGS: {
1821                 struct mdt_body *body;
1822                 struct obd_capa *oc;
1823
1824                 oc = ll_mdscapa_get(inode);
1825                 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
1826                                 OBD_MD_FLFLAGS, 0, &req);
1827                 capa_put(oc);
1828                 if (rc) {
1829                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1830                         RETURN(-abs(rc));
1831                 }
1832
1833                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1834
1835                 flags = body->flags;
1836
1837                 ptlrpc_req_finished(req);
1838
1839                 RETURN(put_user(flags, (int *)arg));
1840         }
1841         case EXT3_IOC_SETFLAGS: {
1842                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1843                 struct obd_info oinfo = { { { 0 } } };
1844                 struct md_op_data *op_data;
1845
1846                 if (get_user(flags, (int *)arg))
1847                         RETURN(-EFAULT);
1848
1849                 oinfo.oi_md = lsm;
1850                 OBDO_ALLOC(oinfo.oi_oa);
1851                 if (!oinfo.oi_oa)
1852                         RETURN(-ENOMEM);
1853
1854                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1855                                              LUSTRE_OPC_ANY, NULL);
1856                 if (IS_ERR(op_data))
1857                         RETURN(PTR_ERR(op_data));
1858
1859                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags = flags;
1860                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
1861                 rc = md_setattr(sbi->ll_md_exp, op_data,
1862                                 NULL, 0, NULL, 0, &req, NULL);
1863                 ll_finish_md_op_data(op_data);
1864                 ptlrpc_req_finished(req);
1865                 if (rc || lsm == NULL) {
1866                         OBDO_FREE(oinfo.oi_oa);
1867                         RETURN(rc);
1868                 }
1869
1870                 oinfo.oi_oa->o_id = lsm->lsm_object_id;
1871                 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
1872                 oinfo.oi_oa->o_flags = flags;
1873                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | 
1874                                        OBD_MD_FLGROUP;
1875                 oinfo.oi_capa = ll_mdscapa_get(inode);
1876
1877                 obdo_from_inode(oinfo.oi_oa, inode,
1878                                 OBD_MD_FLFID | OBD_MD_FLGENER);
1879                 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
1880                 capa_put(oinfo.oi_capa);
1881                 OBDO_FREE(oinfo.oi_oa);
1882                 if (rc) {
1883                         if (rc != -EPERM && rc != -EACCES)
1884                                 CERROR("md_setattr_async fails: rc = %d\n", rc);
1885                         RETURN(rc);
1886                 }
1887
1888                 inode->i_flags = ll_ext_to_inode_flags(flags |
1889                                                        MDS_BFLAG_EXT_FLAGS);
1890                 RETURN(0);
1891         }
1892         default:
1893                 RETURN(-ENOSYS);
1894         }
1895
1896         RETURN(0);
1897 }
1898
1899 int ll_flush_ctx(struct inode *inode)
1900 {
1901         struct ll_sb_info  *sbi = ll_i2sbi(inode);
1902
1903         CDEBUG(D_SEC, "flush context for user %d\n", current->uid);
1904
1905         obd_set_info_async(sbi->ll_md_exp,
1906                            sizeof(KEY_FLUSH_CTX) - 1, KEY_FLUSH_CTX,
1907                            0, NULL, NULL);
1908         obd_set_info_async(sbi->ll_dt_exp,
1909                            sizeof(KEY_FLUSH_CTX) - 1, KEY_FLUSH_CTX,
1910                            0, NULL, NULL);
1911         return 0;
1912 }
1913
1914 /* umount -f client means force down, don't save state */
1915 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1916 void ll_umount_begin(struct vfsmount *vfsmnt, int flags)
1917 {
1918         struct super_block *sb = vfsmnt->mnt_sb;
1919 #else
1920 void ll_umount_begin(struct super_block *sb)
1921 {
1922 #endif
1923         struct lustre_sb_info *lsi = s2lsi(sb);
1924         struct ll_sb_info *sbi = ll_s2sbi(sb);
1925         struct obd_device *obd;
1926         struct obd_ioctl_data ioc_data = { 0 };
1927         ENTRY;
1928
1929 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1930         if (!(flags & MNT_FORCE)) {
1931                 EXIT;
1932                 return;
1933         }
1934 #endif
1935
1936         /* Tell the MGC we got umount -f */
1937         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
1938
1939         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1940                sb->s_count, atomic_read(&sb->s_active));
1941
1942         obd = class_exp2obd(sbi->ll_md_exp);
1943         if (obd == NULL) {
1944                 CERROR("Invalid MDC connection handle "LPX64"\n",
1945                        sbi->ll_md_exp->exp_handle.h_cookie);
1946                 EXIT;
1947                 return;
1948         }
1949         obd->obd_force = 1;
1950         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp, sizeof ioc_data,
1951                       &ioc_data, NULL);
1952
1953         obd = class_exp2obd(sbi->ll_dt_exp);
1954         if (obd == NULL) {
1955                 CERROR("Invalid LOV connection handle "LPX64"\n",
1956                        sbi->ll_dt_exp->exp_handle.h_cookie);
1957                 EXIT;
1958                 return;
1959         }
1960
1961         obd->obd_force = 1;
1962         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp, sizeof ioc_data,
1963                       &ioc_data, NULL);
1964
1965         /* Really, we'd like to wait until there are no requests outstanding,
1966          * and then continue.  For now, we just invalidate the requests,
1967          * schedule, and hope.
1968          */
1969         schedule();
1970
1971         EXIT;
1972 }
1973
1974 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
1975 {
1976         struct ll_sb_info *sbi = ll_s2sbi(sb);
1977         int err;
1978         __u32 read_only;
1979
1980         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1981                 read_only = *flags & MS_RDONLY;
1982                 err = obd_set_info_async(sbi->ll_md_exp,
1983                                          sizeof(KEY_READ_ONLY) - 1,
1984                                          KEY_READ_ONLY, sizeof(read_only),
1985                                          &read_only, NULL);
1986                 if (err) {
1987                         CERROR("Failed to change the read-only flag during "
1988                                "remount: %d\n", err);
1989                         return err;
1990                 }
1991
1992                 if (read_only)
1993                         sb->s_flags |= MS_RDONLY;
1994                 else
1995                         sb->s_flags &= ~MS_RDONLY;
1996         }
1997         return 0;
1998 }
1999
2000 int ll_prep_inode(struct inode **inode,
2001                   struct ptlrpc_request *req,
2002                   struct super_block *sb)
2003 {
2004         struct ll_sb_info *sbi = NULL;
2005         struct lustre_md md;
2006         int rc = 0;
2007         ENTRY;
2008
2009         LASSERT(*inode || sb);
2010         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2011         prune_deathrow(sbi, 1);
2012         memset(&md, 0, sizeof(struct lustre_md));
2013
2014         rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
2015                               sbi->ll_md_exp, &md);
2016         if (rc)
2017                 RETURN(rc);
2018
2019         if (*inode) {
2020                 ll_update_inode(*inode, &md);
2021         } else {
2022                 LASSERT(sb != NULL);
2023
2024                 /*
2025                  * At this point server returns to client's same fid as client
2026                  * generated for creating. So using ->fid1 is okay here.
2027                  */
2028                 LASSERT(fid_is_sane(&md.body->fid1));
2029
2030                 *inode = ll_iget(sb, ll_fid_build_ino(sbi, &md.body->fid1), &md);
2031                 if (*inode == NULL || is_bad_inode(*inode)) {
2032                         if (md.lsm)
2033                                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
2034 #ifdef CONFIG_FS_POSIX_ACL
2035                         if (md.posix_acl) {
2036                                 posix_acl_release(md.posix_acl);
2037                                 md.posix_acl = NULL;
2038                         }
2039 #endif
2040                         rc = -ENOMEM;
2041                         CERROR("new_inode -fatal: rc %d\n", rc);
2042                         GOTO(out, rc);
2043                 }
2044         }
2045
2046         rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp,
2047                          ll_i2info(*inode)->lli_smd);
2048 out:
2049         md_free_lustre_md(sbi->ll_md_exp, &md);
2050         RETURN(rc);
2051 }
2052
2053 char *llap_origins[] = {
2054         [LLAP_ORIGIN_UNKNOWN] = "--",
2055         [LLAP_ORIGIN_READPAGE] = "rp",
2056         [LLAP_ORIGIN_READAHEAD] = "ra",
2057         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
2058         [LLAP_ORIGIN_WRITEPAGE] = "wp",
2059 };
2060
2061 struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
2062                                              struct list_head *list)
2063 {
2064         struct ll_async_page *llap;
2065         struct list_head *pos;
2066
2067         list_for_each(pos, list) {
2068                 if (pos == &sbi->ll_pglist)
2069                         return NULL;
2070                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
2071                 if (llap->llap_page == NULL)
2072                         continue;
2073                 return llap;
2074         }
2075         LBUG();
2076         return NULL;
2077 }
2078
2079 int ll_obd_statfs(struct inode *inode, void *arg)
2080 {
2081         struct ll_sb_info *sbi = NULL;
2082         struct obd_device *client_obd = NULL, *lov_obd = NULL;
2083         struct lov_obd *lov = NULL;
2084         struct obd_statfs stat_buf = {0};
2085         char *buf = NULL;
2086         struct obd_ioctl_data *data = NULL;
2087         __u32 type, index;
2088         int len = 0, rc;
2089
2090         if (!inode || !(sbi = ll_i2sbi(inode)))
2091                 GOTO(out_statfs, rc = -EINVAL);
2092
2093         rc = obd_ioctl_getdata(&buf, &len, arg);
2094         if (rc)
2095                 GOTO(out_statfs, rc);
2096
2097         data = (void*)buf;
2098         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2099             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2100                 GOTO(out_statfs, rc = -EINVAL);
2101
2102         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2103         memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2104
2105         if (type == LL_STATFS_MDC) {
2106                 if (index > 0)
2107                         GOTO(out_statfs, rc = -ENODEV);
2108                 client_obd = class_exp2obd(sbi->ll_md_exp);
2109         } else if (type == LL_STATFS_LOV) {
2110                 lov_obd = class_exp2obd(sbi->ll_dt_exp);
2111                 lov = &lov_obd->u.lov;
2112
2113                 if ((index >= lov->desc.ld_tgt_count))
2114                         GOTO(out_statfs, rc = -ENODEV);
2115                 if (!lov->lov_tgts[index])
2116                         /* Try again with the next index */
2117                         GOTO(out_statfs, rc = -EAGAIN);
2118
2119                 client_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2120                 if (!lov->lov_tgts[index]->ltd_active)
2121                         GOTO(out_uuid, rc = -ENODATA);
2122         }
2123
2124         if (!client_obd)
2125                 GOTO(out_statfs, rc = -EINVAL);
2126
2127         rc = obd_statfs(client_obd, &stat_buf, cfs_time_current_64() - 1);
2128         if (rc)
2129                 GOTO(out_statfs, rc);
2130
2131         if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
2132                 GOTO(out_statfs, rc = -EFAULT);
2133
2134 out_uuid:
2135         if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(client_obd),
2136                          data->ioc_plen2))
2137                 rc = -EFAULT;
2138
2139 out_statfs:
2140         if (buf)
2141                 obd_ioctl_freedata(buf, len);
2142         return rc;
2143 }
2144
2145 int ll_process_config(struct lustre_cfg *lcfg)
2146 {
2147         char *ptr;
2148         void *sb;
2149         struct lprocfs_static_vars lvars;
2150         unsigned long x; 
2151         int rc = 0;
2152
2153         lprocfs_llite_init_vars(&lvars);
2154
2155         /* The instance name contains the sb: lustre-client-aacfe000 */
2156         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2157         if (!ptr || !*(++ptr)) 
2158                 return -EINVAL;
2159         if (sscanf(ptr, "%lx", &x) != 1)
2160                 return -EINVAL;
2161         sb = (void *)x;
2162         /* This better be a real Lustre superblock! */
2163         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2164
2165         /* Note we have not called client_common_fill_super yet, so 
2166            proc fns must be able to handle that! */
2167         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2168                                       lcfg, sb);
2169         return(rc);
2170 }
2171
2172 /* this function prepares md_op_data hint for passing ot down to MD stack. */
2173 struct md_op_data * ll_prep_md_op_data(struct md_op_data *op_data,
2174                                        struct inode *i1, struct inode *i2,
2175                                        const char *name, int namelen,
2176                                        int mode, __u32 opc, void *data)
2177 {
2178         LASSERT(i1 != NULL);
2179
2180         if (namelen > ll_i2sbi(i1)->ll_namelen)
2181                 return ERR_PTR(-ENAMETOOLONG);
2182         
2183         if (op_data == NULL)
2184                 OBD_ALLOC_PTR(op_data);
2185         
2186         if (op_data == NULL)
2187                 return ERR_PTR(-ENOMEM);
2188
2189         ll_i2gids(op_data->op_suppgids, i1, i2);
2190         op_data->op_fid1 = *ll_inode2fid(i1);
2191         op_data->op_capa1 = ll_mdscapa_get(i1);
2192
2193         if (i2) {
2194                 op_data->op_fid2 = *ll_inode2fid(i2);
2195                 op_data->op_capa2 = ll_mdscapa_get(i2);
2196         } else {
2197                 fid_zero(&op_data->op_fid2);
2198         }
2199
2200         op_data->op_name = name;
2201         op_data->op_namelen = namelen;
2202         op_data->op_mode = mode;
2203         op_data->op_mod_time = CURRENT_SECONDS;
2204         op_data->op_fsuid = current->fsuid;
2205         op_data->op_fsgid = current->fsgid;
2206         op_data->op_cap = current->cap_effective;
2207         op_data->op_bias = MDS_CHECK_SPLIT;
2208         op_data->op_opc = opc;
2209         op_data->op_mds = 0;
2210         op_data->op_data = data;
2211
2212         return op_data;
2213 }
2214
2215 void ll_finish_md_op_data(struct md_op_data *op_data)
2216 {
2217         capa_put(op_data->op_capa1);
2218         capa_put(op_data->op_capa2);
2219         OBD_FREE_PTR(op_data);
2220 }