Whamcloud - gitweb
LU-73 b=22375 RHEL6 kernel support for 1.8
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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/llite_lib.c
37  *
38  * Lustre Light Super operations
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <linux/module.h>
44 #include <linux/types.h>
45 #include <linux/random.h>
46 #include <linux/version.h>
47
48 #include <lustre_lite.h>
49 #include <lustre_ha.h>
50 #include <lustre_dlm.h>
51 #include <lprocfs_status.h>
52 #include <lustre_disk.h>
53 #include <lustre_param.h>
54 #include <lustre_cache.h>
55 #include <obd_support.h>
56 #include "llite_internal.h"
57
58 cfs_mem_cache_t *ll_file_data_slab;
59
60 LIST_HEAD(ll_super_blocks);
61 struct rw_semaphore ll_sb_sem;
62
63 extern struct address_space_operations ll_aops;
64 extern struct address_space_operations ll_dir_aops;
65
66 #ifndef log2
67 #define log2(n) ffz(~(n))
68 #endif
69
70 static void ll_pglist_fini(struct ll_sb_info *sbi)
71 {
72         struct page *page;
73         int i;
74
75         if (sbi->ll_pglist == NULL)
76                 return;
77
78         for_each_possible_cpu(i) {
79                 page = sbi->ll_pglist[i]->llpd_page;
80                 if (page) {
81                         sbi->ll_pglist[i] = NULL;
82                         __free_page(page);
83                 }
84         }
85
86         OBD_FREE(sbi->ll_pglist, sizeof(void *)*num_possible_cpus());
87         sbi->ll_pglist = NULL;
88 }
89
90 static int ll_pglist_init(struct ll_sb_info *sbi)
91 {
92         struct ll_pglist_data *pd;
93         unsigned long budget;
94         int i, color = 0;
95         ENTRY;
96
97         OBD_ALLOC(sbi->ll_pglist, sizeof(void *) * num_possible_cpus());
98         if (sbi->ll_pglist == NULL)
99                 RETURN(-ENOMEM);
100
101         budget = sbi->ll_async_page_max / num_online_cpus();
102         for_each_possible_cpu(i) {
103                 struct page *page = alloc_pages_node(cpu_to_node(i),
104                                                     GFP_KERNEL, 0);
105                 if (page == NULL) {
106                         ll_pglist_fini(sbi);
107                         RETURN(-ENOMEM);
108                 }
109
110                 if (color + L1_CACHE_ALIGN(sizeof(*pd)) > PAGE_SIZE)
111                         color = 0;
112
113                 pd = (struct ll_pglist_data *)(page_address(page) + color);
114                 memset(pd, 0, sizeof(*pd));
115                 spin_lock_init(&pd->llpd_lock);
116                 INIT_LIST_HEAD(&pd->llpd_list);
117                 if (cpu_online(i))
118                         pd->llpd_budget = budget;
119                 pd->llpd_cpu = i;
120                 pd->llpd_page = page;
121                 atomic_set(&pd->llpd_sample_count, 0);
122                 sbi->ll_pglist[i] = pd;
123                 color += L1_CACHE_ALIGN(sizeof(*pd));
124         }
125
126         RETURN(0);
127 }
128
129 static struct ll_sb_info *ll_init_sbi(void)
130 {
131         struct ll_sb_info *sbi = NULL;
132         unsigned long pages;
133         struct sysinfo si;
134         class_uuid_t uuid;
135         int i;
136         ENTRY;
137
138         OBD_ALLOC(sbi, sizeof(*sbi));
139         if (!sbi)
140                 RETURN(NULL);
141
142         OBD_ALLOC(sbi->ll_async_page_sample, sizeof(long)*num_possible_cpus());
143         if (sbi->ll_async_page_sample == NULL)
144                 GOTO(out, 0);
145
146         spin_lock_init(&sbi->ll_lock);
147         init_mutex(&sbi->ll_lco.lco_lock);
148         spin_lock_init(&sbi->ll_pp_extent_lock);
149         spin_lock_init(&sbi->ll_process_lock);
150         sbi->ll_rw_stats_on = 0;
151
152         si_meminfo(&si);
153         pages = si.totalram - si.totalhigh;
154         if (pages >> (20 - CFS_PAGE_SHIFT) < 512) {
155 #ifdef HAVE_BGL_SUPPORT
156                 sbi->ll_async_page_max = pages / 4;
157 #else
158                 sbi->ll_async_page_max = pages / 2;
159 #endif
160         } else {
161                 sbi->ll_async_page_max = (pages / 4) * 3;
162         }
163
164         lcounter_init(&sbi->ll_async_page_count);
165         spin_lock_init(&sbi->ll_async_page_reblnc_lock);
166         sbi->ll_async_page_sample_max = 64 * num_online_cpus();
167         sbi->ll_async_page_reblnc_count = 0;
168         sbi->ll_async_page_clock_hand = 0;
169         if (ll_pglist_init(sbi))
170                 GOTO(out, 0);
171
172         sbi->ll_ra_info.ra_max_pages_per_file = min(pages / 32,
173                                            SBI_DEFAULT_READAHEAD_MAX);
174         sbi->ll_ra_info.ra_max_pages = sbi->ll_ra_info.ra_max_pages_per_file;
175         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
176                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
177         sbi->ll_contention_time = SBI_DEFAULT_CONTENTION_SECONDS;
178         sbi->ll_lockless_truncate_enable = SBI_DEFAULT_LOCKLESS_TRUNCATE_ENABLE;
179         sbi->ll_lockless_direct_io = SBI_DEFAULT_LOCKLESS_DIRECT_IO;
180         sbi->ll_direct_io_default = SBI_DEFAULT_DIRECT_IO_DEFAULT;
181         INIT_LIST_HEAD(&sbi->ll_conn_chain);
182         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
183
184         ll_generate_random_uuid(uuid);
185         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
186         CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
187
188         down_write(&ll_sb_sem);
189         list_add_tail(&sbi->ll_list, &ll_super_blocks);
190         up_write(&ll_sb_sem);
191
192 #ifdef ENABLE_CHECKSUM
193         sbi->ll_flags |= LL_SBI_DATA_CHECKSUM;
194 #endif
195 #ifdef ENABLE_LLITE_CHECKSUM
196         sbi->ll_flags |= LL_SBI_LLITE_CHECKSUM;
197 #endif
198
199 #ifdef HAVE_LRU_RESIZE_SUPPORT
200         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
201 #endif
202
203         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
204                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_r_hist.oh_lock);
205                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_w_hist.oh_lock);
206         }
207
208         /* metadata statahead is disabled by default */
209         sbi->ll_sa_max = 0;
210         /* sbi->ll_sa_max = LL_SA_RPC_DEF; */
211
212         RETURN(sbi);
213
214 out:
215         if (sbi->ll_async_page_sample)
216                 OBD_FREE(sbi->ll_async_page_sample,
217                          sizeof(long) * num_possible_cpus());
218         ll_pglist_fini(sbi);
219         OBD_FREE(sbi, sizeof(*sbi));
220         RETURN(NULL);
221 }
222
223 void ll_free_sbi(struct super_block *sb)
224 {
225         struct ll_sb_info *sbi = ll_s2sbi(sb);
226         ENTRY;
227
228         if (sbi != NULL) {
229                 down_write(&ll_sb_sem);
230                 list_del(&sbi->ll_list);
231                 up_write(&ll_sb_sem);
232                 /* dont allow find cache via sb list first */
233                 ll_pglist_fini(sbi);
234                 lcounter_destroy(&sbi->ll_async_page_count);
235                 OBD_FREE(sbi->ll_async_page_sample,
236                          sizeof(long) * num_possible_cpus());
237                 OBD_FREE(sbi, sizeof(*sbi));
238         }
239         EXIT;
240 }
241
242 static struct dentry_operations ll_d_root_ops = {
243         .d_compare = ll_dcompare,
244         .d_revalidate = ll_revalidate_nd,
245 };
246
247 static int client_common_fill_super(struct super_block *sb,
248                                     char *mdc, char *osc)
249 {
250         struct inode *root = 0;
251         struct ll_sb_info *sbi = ll_s2sbi(sb);
252         struct obd_device *obd;
253         struct ll_fid rootfid;
254         struct obd_statfs osfs;
255         struct ptlrpc_request *request = NULL;
256         struct lustre_handle osc_conn = {0, };
257         struct lustre_handle mdc_conn = {0, };
258         struct lustre_md md;
259         struct obd_connect_data *data = NULL;
260         int err, checksum;
261         ENTRY;
262
263         obd = class_name2obd(mdc);
264         if (!obd) {
265                 CERROR("MDC %s: not setup or attached\n", mdc);
266                 RETURN(-EINVAL);
267         }
268
269         OBD_ALLOC(data, sizeof(*data));
270         if (data == NULL)
271                 RETURN(-ENOMEM);
272
273         if (proc_lustre_fs_root) {
274                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
275                                                   osc, mdc);
276                 if (err < 0)
277                         CERROR("could not register mount in /proc/fs/lustre\n");
278         }
279
280         /* indicate the features supported by this client */
281         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_IBITS      |
282                                   OBD_CONNECT_JOIN    | OBD_CONNECT_ATTRFID    |
283                                   OBD_CONNECT_NODEVOH | OBD_CONNECT_CANCELSET  |
284                                   OBD_CONNECT_AT      | OBD_CONNECT_FID |
285                                   OBD_CONNECT_VBR     | OBD_CONNECT_LOV_V3;
286 #ifdef HAVE_LRU_RESIZE_SUPPORT
287         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
288                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
289 #endif
290 #ifdef CONFIG_FS_POSIX_ACL
291         data->ocd_connect_flags |= OBD_CONNECT_ACL;
292 #endif
293         data->ocd_ibits_known = MDS_INODELOCK_FULL;
294         data->ocd_version = LUSTRE_VERSION_CODE;
295
296         if (sb->s_flags & MS_RDONLY)
297                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
298         if (sbi->ll_flags & LL_SBI_USER_XATTR)
299                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
300
301 #ifdef HAVE_MS_FLOCK_LOCK
302         /* force vfs to use lustre handler for flock() calls - bug 10743 */
303         sb->s_flags |= MS_FLOCK_LOCK;
304 #endif
305
306         if (sbi->ll_flags & LL_SBI_FLOCK)
307                 sbi->ll_fop = &ll_file_operations_flock;
308         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
309                 sbi->ll_fop = &ll_file_operations;
310         else
311                 sbi->ll_fop = &ll_file_operations_noflock;
312
313
314         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, data, &sbi->ll_mdc_exp);
315         if (err == -EBUSY) {
316                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (mdc %s) is performing "
317                                    "recovery, of which this client is not a "
318                                    "part. Please wait for recovery to complete,"
319                                    " abort, or time out.\n", mdc);
320                 GOTO(out, err);
321         } else if (err) {
322                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
323                 GOTO(out, err);
324         }
325
326         err = obd_fid_init(sbi->ll_mdc_exp);
327         if (err)
328                 GOTO(out_mdc, err);
329
330         err = obd_statfs(obd, &osfs,
331                          cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
332         if (err)
333                 GOTO(out_mdc, err);
334
335         /* MDC connect is surely finished by now because we actually sent
336          * a statfs RPC, otherwise obd_connect() is asynchronous. */
337         *data = class_exp2cliimp(sbi->ll_mdc_exp)->imp_connect_data;
338
339         LASSERT(osfs.os_bsize);
340         sb->s_blocksize = osfs.os_bsize;
341         sb->s_blocksize_bits = log2(osfs.os_bsize);
342         sb->s_magic = LL_SUPER_MAGIC;
343
344         /* for bug 11559. in $LINUX/fs/read_write.c, function do_sendfile():
345          *         retval = in_file->f_op->sendfile(...);
346          *         if (*ppos > max)
347          *                 retval = -EOVERFLOW;
348          *
349          * it will check if *ppos is greater than max. However, max equals to
350          * s_maxbytes, which is a negative integer in a x86_64 box since loff_t
351          * has been defined as a signed long long ineger in linux kernel. */
352 #if BITS_PER_LONG == 64
353         sb->s_maxbytes = PAGE_CACHE_MAXBYTES >> 1;
354 #else
355         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
356 #endif
357         sbi->ll_namelen = osfs.os_namelen;
358         sbi->ll_max_rw_chunk = LL_DEFAULT_MAX_RW_CHUNK;
359
360         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
361             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
362                 LCONSOLE_INFO("Disabling user_xattr feature because "
363                               "it is not supported on the server\n");
364                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
365         }
366
367         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
368 #ifdef MS_POSIXACL
369                 sb->s_flags |= MS_POSIXACL;
370 #endif
371                 sbi->ll_flags |= LL_SBI_ACL;
372         } else
373                 sbi->ll_flags &= ~LL_SBI_ACL;
374
375         if (data->ocd_connect_flags & OBD_CONNECT_JOIN)
376                 sbi->ll_flags |= LL_SBI_JOIN;
377
378         obd = class_name2obd(osc);
379         if (!obd) {
380                 CERROR("OSC %s: not setup or attached\n", osc);
381                 GOTO(out_mdc, err = -ENODEV);
382         }
383
384         data->ocd_connect_flags = OBD_CONNECT_VERSION   | OBD_CONNECT_GRANT    |
385                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
386                                   OBD_CONNECT_SRVLOCK   | OBD_CONNECT_CANCELSET|
387                                   OBD_CONNECT_AT        | OBD_CONNECT_FID      |
388                                   OBD_CONNECT_VBR       | OBD_CONNECT_TRUNCLOCK;
389
390         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_CKSUM)) {
391                 /* OBD_CONNECT_CKSUM should always be set, even if checksums are
392                  * disabled by default, because it can still be enabled on the
393                  * fly via /proc. As a consequence, we still need to come to an
394                  * agreement on the supported algorithms at connect time */
395                 data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
396
397                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
398                         data->ocd_cksum_types = OBD_CKSUM_ADLER;
399                 else
400                         /* send the list of supported checksum types */
401                         data->ocd_cksum_types = OBD_CKSUM_ALL;
402         }
403
404 #ifdef HAVE_LRU_RESIZE_SUPPORT
405         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
406                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
407 #endif
408
409         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
410                "ocd_grant: %d\n", data->ocd_connect_flags,
411                data->ocd_version, data->ocd_grant);
412
413         obd->obd_upcall.onu_owner = &sbi->ll_lco;
414         obd->obd_upcall.onu_upcall = ll_ocd_update;
415         data->ocd_brw_size = PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT;
416
417         obd_register_lock_cancel_cb(obd, ll_extent_lock_cancel_cb);
418         obd_register_page_removal_cb(obd, ll_page_removal_cb, ll_pin_extent_cb);
419
420         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, data, &sbi->ll_osc_exp);
421         if (err == -EBUSY) {
422                 LCONSOLE_ERROR_MSG(0x150, "An OST (osc %s) is performing "
423                                    "recovery, of which this client is not a "
424                                    "part.  Please wait for recovery to "
425                                    "complete, abort, or time out.\n", osc);
426                 GOTO(out, err);
427         } else if (err) {
428                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
429                 GOTO(out_cb, err);
430         }
431
432         mutex_down(&sbi->ll_lco.lco_lock);
433         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
434         sbi->ll_lco.lco_mdc_exp = sbi->ll_mdc_exp;
435         sbi->ll_lco.lco_osc_exp = sbi->ll_osc_exp;
436         mutex_up(&sbi->ll_lco.lco_lock);
437
438         err = mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
439         if (err) {
440                 CERROR("cannot set max EA and cookie sizes: rc = %d\n", err);
441                 GOTO(out_osc, err);
442         }
443
444         err = obd_prep_async_page(sbi->ll_osc_exp, NULL, NULL, NULL,
445                                   0, NULL, NULL, NULL, 0, NULL);
446         if (err < 0) {
447                 LCONSOLE_ERROR_MSG(0x151, "There are no OST's in this "
448                                    "filesystem. There must be at least one "
449                                    "active OST for a client to start.\n");
450                 GOTO(out_osc, err);
451         }
452
453         if (!ll_async_page_slab) {
454                 ll_async_page_slab_size =
455                         size_round(sizeof(struct ll_async_page)) + err;
456                 ll_async_page_slab = cfs_mem_cache_create("ll_async_page",
457                                                           ll_async_page_slab_size,
458                                                           0, 0);
459                 if (!ll_async_page_slab)
460                         GOTO(out_osc, err = -ENOMEM);
461         }
462
463         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
464         if (err) {
465                 CERROR("cannot mds_connect: rc = %d\n", err);
466                 GOTO(out_osc, err);
467         }
468         CDEBUG(D_SUPER, "rootfid "LPU64":"DFID"\n", rootfid.id,
469                         PFID((struct lu_fid*)&rootfid));
470         sbi->ll_rootino = rootfid.id;
471
472         sb->s_op = &lustre_super_operations;
473 #if THREAD_SIZE >= 8192
474         /* Disable the NFS export because of stack overflow
475          * when THREAD_SIZE < 8192. Please refer to 17630. */
476         sb->s_export_op = &lustre_export_operations;
477 #endif
478
479         /* make root inode
480          * XXX: move this to after cbd setup? */
481         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
482                           OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS |
483                           (sbi->ll_flags & LL_SBI_ACL ? OBD_MD_FLACL : 0),
484                           0, &request);
485         if (err) {
486                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
487                 GOTO(out_osc, err);
488         }
489
490         err = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
491         if (err) {
492                 CERROR("failed to understand root inode md: rc = %d\n",err);
493                 ptlrpc_req_finished (request);
494                 GOTO(out_osc, err);
495         }
496
497         LASSERT(sbi->ll_rootino != 0);
498         root = ll_iget(sb, ll_fid_build_ino(&rootfid), &md);
499
500         ptlrpc_req_finished(request);
501
502         if (root == NULL || is_bad_inode(root)) {
503                 mdc_free_lustre_md(sbi->ll_osc_exp, &md);
504                 CERROR("lustre_lite: bad iget4 for root\n");
505                 GOTO(out_root, err = -EBADF);
506         }
507
508         err = ll_close_thread_start(&sbi->ll_lcq);
509         if (err) {
510                 CERROR("cannot start close thread: rc %d\n", err);
511                 GOTO(out_root, err);
512         }
513
514         checksum = sbi->ll_flags & LL_SBI_DATA_CHECKSUM;
515         err = obd_set_info_async(sbi->ll_osc_exp, sizeof(KEY_CHECKSUM),
516                                  KEY_CHECKSUM, sizeof(checksum),
517                                  &checksum, NULL);
518
519         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
520            backing dev info assigned to inode mapping is used for
521            determining maximal readahead. */
522 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
523     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
524         /* bug 2805 - set VM readahead to zero */
525         vm_max_readahead = vm_min_readahead = 0;
526 #endif
527
528         sb->s_root = d_alloc_root(root);
529         if (data != NULL)
530                 OBD_FREE(data, sizeof(*data));
531         sb->s_root->d_op = &ll_d_root_ops;
532
533         sbi->ll_sdev_orig = sb->s_dev;
534         /* We set sb->s_dev equal on all lustre clients in order to support
535          * NFS export clustering.  NFSD requires that the FSID be the same
536          * on all clients. */
537         /* s_dev is also used in lt_compare() to compare two fs, but that is
538          * only a node-local comparison. */
539         sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_target_uuid.uuid,
540                                  strlen(sbi2mdc(sbi)->cl_target_uuid.uuid));
541
542         RETURN(err);
543
544 out_root:
545         if (root)
546                 iput(root);
547 out_osc:
548         obd_disconnect(sbi->ll_osc_exp);
549         sbi->ll_osc_exp = NULL;
550 out_cb:
551         obd = class_name2obd(osc);
552         obd_unregister_lock_cancel_cb(obd, ll_extent_lock_cancel_cb);
553         obd_unregister_page_removal_cb(obd, ll_page_removal_cb);
554 out_mdc:
555         obd_fid_fini(sbi->ll_mdc_exp);
556         obd_disconnect(sbi->ll_mdc_exp);
557         sbi->ll_mdc_exp = NULL;
558 out:
559         if (data != NULL)
560                 OBD_FREE(data, sizeof(*data));
561         lprocfs_unregister_mountpoint(sbi);
562         RETURN(err);
563 }
564
565 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
566 {
567         int size, rc;
568
569         *lmmsize = obd_size_diskmd(sbi->ll_osc_exp, NULL);
570         size = sizeof(int);
571         rc = obd_get_info(sbi->ll_mdc_exp, sizeof(KEY_MAX_EASIZE),
572                           KEY_MAX_EASIZE, &size, lmmsize, NULL);
573         if (rc)
574                 CERROR("Get max mdsize error rc %d \n", rc);
575
576         RETURN(rc);
577 }
578
579 void ll_dump_inode(struct inode *inode)
580 {
581         struct list_head *tmp;
582         int dentry_count = 0;
583
584         LASSERT(inode != NULL);
585
586         list_for_each(tmp, &inode->i_dentry)
587                 dentry_count++;
588
589         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
590                inode, ll_i2mdcexp(inode)->exp_obd->obd_name, inode->i_ino,
591                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
592 }
593
594 void lustre_dump_dentry(struct dentry *dentry, int recur)
595 {
596         struct list_head *tmp;
597         int subdirs = 0;
598
599         LASSERT(dentry != NULL);
600
601         list_for_each(tmp, &dentry->d_subdirs)
602                 subdirs++;
603
604         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
605                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
606                dentry->d_name.len, dentry->d_name.name,
607                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
608                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
609                dentry->d_flags, dentry->d_fsdata, subdirs);
610         if (dentry->d_inode != NULL)
611                 ll_dump_inode(dentry->d_inode);
612
613         if (recur == 0)
614                 return;
615
616         list_for_each(tmp, &dentry->d_subdirs) {
617                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
618                 lustre_dump_dentry(d, recur - 1);
619         }
620 }
621
622 void client_common_put_super(struct super_block *sb)
623 {
624         struct ll_sb_info *sbi = ll_s2sbi(sb);
625         ENTRY;
626
627         ll_close_thread_shutdown(sbi->ll_lcq);
628
629         lprocfs_unregister_mountpoint(sbi);
630
631         list_del(&sbi->ll_conn_chain);
632
633         /* callbacks is cleared after disconnect each target */
634         obd_disconnect(sbi->ll_osc_exp);
635         sbi->ll_osc_exp = NULL;
636
637         obd_fid_fini(sbi->ll_mdc_exp);
638         obd_disconnect(sbi->ll_mdc_exp);
639         sbi->ll_mdc_exp = NULL;
640
641         EXIT;
642 }
643
644 void ll_kill_super(struct super_block *sb)
645 {
646         struct ll_sb_info *sbi;
647
648         ENTRY;
649
650         /* not init sb ?*/
651         if (!(sb->s_flags & MS_ACTIVE))
652                 return;
653
654         sbi = ll_s2sbi(sb);
655         /* we need restore s_dev from changed for clustred NFS before put_super
656          * because new kernels have cached s_dev and change sb->s_dev in
657          * put_super not affected real removing devices */
658         if (sbi)
659                 sb->s_dev = sbi->ll_sdev_orig;
660         EXIT;
661 }
662
663 char *ll_read_opt(const char *opt, char *data)
664 {
665         char *value;
666         char *retval;
667         ENTRY;
668
669         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
670         if (strncmp(opt, data, strlen(opt)))
671                 RETURN(NULL);
672         if ((value = strchr(data, '=')) == NULL)
673                 RETURN(NULL);
674
675         value++;
676         OBD_ALLOC(retval, strlen(value) + 1);
677         if (!retval) {
678                 CERROR("out of memory!\n");
679                 RETURN(NULL);
680         }
681
682         memcpy(retval, value, strlen(value)+1);
683         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
684         RETURN(retval);
685 }
686
687 static inline int ll_set_opt(const char *opt, char *data, int fl)
688 {
689         if (strncmp(opt, data, strlen(opt)) != 0)
690                 return(0);
691         else
692                 return(fl);
693 }
694
695 /* non-client-specific mount options are parsed in lmd_parse */
696 static int ll_options(char *options, int *flags)
697 {
698         int tmp;
699         char *s1 = options, *s2;
700         ENTRY;
701
702         if (!options)
703                 RETURN(0);
704
705         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
706
707         while (*s1) {
708                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
709                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
710                 if (tmp) {
711                         *flags |= tmp;
712                         goto next;
713                 }
714                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
715                 if (tmp) {
716                         *flags |= tmp;
717                         goto next;
718                 }
719                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
720                 if (tmp) {
721                         *flags |= tmp;
722                         goto next;
723                 }
724                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
725                 if (tmp) {
726                         *flags &= ~tmp;
727                         goto next;
728                 }
729                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
730                 if (tmp) {
731                         *flags |= tmp;
732                         goto next;
733                 }
734                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
735                 if (tmp) {
736                         *flags &= ~tmp;
737                         goto next;
738                 }
739                 tmp = ll_set_opt("acl", s1, LL_SBI_ACL);
740                 if (tmp) {
741                         /* Ignore deprecated mount option.  The client will
742                          * always try to mount with ACL support, whether this
743                          * is used depends on whether server supports it. */
744                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
745                                                   "mount option 'acl'.\n");
746                         goto next;
747                 }
748                 tmp = ll_set_opt("noacl", s1, LL_SBI_ACL);
749                 if (tmp) {
750                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
751                                                   "mount option 'noacl'.\n");
752                         goto next;
753                 }
754
755                 tmp = ll_set_opt("checksum", s1, LL_SBI_DATA_CHECKSUM);
756                 if (tmp) {
757                         *flags |= tmp;
758                         goto next;
759                 }
760                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_DATA_CHECKSUM);
761                 if (tmp) {
762                         *flags &= ~tmp;
763                         goto next;
764                 }
765
766                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
767                 if (tmp) {
768                         *flags |= tmp;
769                         goto next;
770                 }
771                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
772                 if (tmp) {
773                         *flags &= ~tmp;
774                         goto next;
775                 }
776                 tmp = ll_set_opt("lazystatfs", s1, LL_SBI_LAZYSTATFS);
777                 if (tmp) {
778                         *flags |= tmp;
779                         goto next;
780                 }
781                 tmp = ll_set_opt("nolazystatfs", s1, LL_SBI_LAZYSTATFS);
782                 if (tmp) {
783                         *flags &= ~tmp;
784                         goto next;
785                 }
786                 tmp = ll_set_opt("32bitapi", s1, LL_SBI_32BIT_API);
787                 if (tmp) {
788                         *flags |= tmp;
789                         goto next;
790                 }
791                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
792                                    s1);
793                 RETURN(-EINVAL);
794
795 next:
796                 /* Find next opt */
797                 s2 = strchr(s1, ',');
798                 if (s2 == NULL)
799                         break;
800                 s1 = s2 + 1;
801         }
802         RETURN(0);
803 }
804
805 void ll_lli_init(struct ll_inode_info *lli)
806 {
807         lli->lli_inode_magic = LLI_INODE_MAGIC;
808         sema_init(&lli->lli_size_sem, 1);
809         sema_init(&lli->lli_write_sem, 1);
810         lli->lli_flags = 0;
811         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
812         spin_lock_init(&lli->lli_lock);
813         sema_init(&lli->lli_och_sem, 1);
814         lli->lli_mds_read_och = lli->lli_mds_write_och = NULL;
815         lli->lli_mds_exec_och = NULL;
816         lli->lli_open_fd_read_count = lli->lli_open_fd_write_count = 0;
817         lli->lli_open_fd_exec_count = 0;
818         INIT_LIST_HEAD(&lli->lli_dead_list);
819 #ifdef HAVE_CLOSE_THREAD
820         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
821 #endif
822         init_rwsem(&lli->lli_truncate_rwsem);
823 }
824
825 /* COMPAT_146 */
826 #define MDCDEV "mdc_dev"
827 static int old_lustre_process_log(struct super_block *sb, char *newprofile,
828                                   struct config_llog_instance *cfg)
829 {
830         struct lustre_sb_info *lsi = s2lsi(sb);
831         struct obd_device *obd;
832         struct lustre_handle mdc_conn = {0, };
833         struct obd_export *exp;
834         char *ptr, *mdt, *profile;
835         char niduuid[10] = "mdtnid0";
836         class_uuid_t uuid;
837         struct obd_uuid mdc_uuid;
838         struct llog_ctxt *ctxt;
839         struct obd_connect_data ocd = { 0 };
840         lnet_nid_t nid;
841         int i, rc = 0, recov_bk = 1, failnodes = 0;
842         ENTRY;
843
844         ll_generate_random_uuid(uuid);
845         class_uuid_unparse(uuid, &mdc_uuid);
846         CDEBUG(D_HA, "generated uuid: %s\n", mdc_uuid.uuid);
847
848         /* Figure out the old mdt and profile name from new-style profile
849            ("lustre" from "mds/lustre-client") */
850         mdt = newprofile;
851         profile = strchr(mdt, '/');
852         if (profile == NULL) {
853                 CDEBUG(D_CONFIG, "Can't find MDT name in %s\n", newprofile);
854                 GOTO(out, rc = -EINVAL);
855         }
856         *profile = '\0';
857         profile++;
858         ptr = strrchr(profile, '-');
859         if (ptr == NULL) {
860                 CDEBUG(D_CONFIG, "Can't find client name in %s\n", newprofile);
861                 GOTO(out, rc = -EINVAL);
862         }
863         *ptr = '\0';
864
865         LCONSOLE_WARN("This looks like an old mount command; I will try to "
866                       "contact MDT '%s' for profile '%s'\n", mdt, profile);
867
868         /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
869         i = 0;
870         ptr = lsi->lsi_lmd->lmd_dev;
871         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
872                 rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid, 0,0,0);
873                 i++;
874                 /* Stop at the first failover nid */
875                 if (*ptr == ':')
876                         break;
877         }
878         if (i == 0) {
879                 CERROR("No valid MDT nids found.\n");
880                 GOTO(out, rc = -EINVAL);
881         }
882         failnodes++;
883
884         rc = do_lcfg(MDCDEV, 0, LCFG_ATTACH, LUSTRE_MDC_NAME,mdc_uuid.uuid,0,0);
885         if (rc < 0)
886                 GOTO(out_del_uuid, rc);
887
888         rc = do_lcfg(MDCDEV, 0, LCFG_SETUP, mdt, niduuid, 0, 0);
889         if (rc < 0) {
890                 LCONSOLE_ERROR_MSG(0x153, "I couldn't establish a connection "
891                                    "with the MDT. Check that the MDT host NID "
892                                    "is correct and the networks are up.\n");
893                 GOTO(out_detach, rc);
894         }
895
896         obd = class_name2obd(MDCDEV);
897         if (obd == NULL)
898                 GOTO(out_cleanup, rc = -EINVAL);
899
900         /* Add any failover nids */
901         while (*ptr == ':') {
902                 /* New failover node */
903                 sprintf(niduuid, "mdtnid%d", failnodes);
904                 i = 0;
905                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
906                         i++;
907                         rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid,0,0,0);
908                         if (rc)
909                                 CERROR("Add uuid for %s failed %d\n",
910                                        libcfs_nid2str(nid), rc);
911                         if (*ptr == ':')
912                                 break;
913                 }
914                 if (i > 0) {
915                         rc = do_lcfg(MDCDEV, 0, LCFG_ADD_CONN, niduuid, 0, 0,0);
916                         if (rc)
917                                 CERROR("Add conn for %s failed %d\n",
918                                        libcfs_nid2str(nid), rc);
919                         failnodes++;
920                 } else {
921                         /* at ":/fsname" */
922                         break;
923                 }
924         }
925
926         /* Try all connections, but only once. */
927         rc = obd_set_info_async(obd->obd_self_export,
928                                 sizeof(KEY_INIT_RECOV_BACKUP), KEY_INIT_RECOV_BACKUP,
929                                 sizeof(recov_bk), &recov_bk, NULL);
930         if (rc)
931                 GOTO(out_cleanup, rc);
932
933         /* If we don't have this then an ACL MDS will refuse the connection */
934         ocd.ocd_connect_flags = OBD_CONNECT_ACL;
935
936         rc = obd_connect(&mdc_conn, obd, &mdc_uuid, &ocd, &exp);
937         if (rc) {
938                 CERROR("cannot connect to %s: rc = %d\n", mdt, rc);
939                 GOTO(out_cleanup, rc);
940         }
941
942         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
943
944         cfg->cfg_flags |= CFG_F_COMPAT146;
945
946 #if 1
947         rc = class_config_parse_llog(ctxt, profile, cfg);
948 #else
949         /*
950          * For debugging, it's useful to just dump the log
951          */
952         rc = class_config_dump_llog(ctxt, profile, cfg);
953 #endif
954         llog_ctxt_put(ctxt);
955         switch (rc) {
956         case 0: {
957                 /* Set the caller's profile name to the old-style */
958                 memcpy(newprofile, profile, strlen(profile) + 1);
959                 break;
960         }
961         case -EINVAL:
962                 LCONSOLE_ERROR_MSG(0x154, "%s: The configuration '%s' could not"
963                                    " be read from the MDT '%s'.  Make sure this"
964                                    " client and the MDT are running compatible "
965                                    "versions of Lustre.\n",
966                                    obd->obd_name, profile, mdt);
967                 /* fall through */
968         default:
969                 LCONSOLE_ERROR_MSG(0x155, "%s: The configuration '%s' could not"
970                                    " be read from the MDT '%s'.  This may be "
971                                    "the result of communication errors between "
972                                    "the client and the MDT, or if the MDT is "
973                                    "not running.\n", obd->obd_name, profile,
974                                    mdt);
975                 break;
976         }
977
978         /* We don't so much care about errors in cleaning up the config llog
979          * connection, as we have already read the config by this point. */
980         obd_disconnect(exp);
981
982 out_cleanup:
983         do_lcfg(MDCDEV, 0, LCFG_CLEANUP, 0, 0, 0, 0);
984
985 out_detach:
986         do_lcfg(MDCDEV, 0, LCFG_DETACH, 0, 0, 0, 0);
987
988 out_del_uuid:
989         /* class_add_uuid adds a nid even if the same uuid exists; we might
990            delete any copy here.  So they all better match. */
991         for (i = 0; i < failnodes; i++) {
992                 sprintf(niduuid, "mdtnid%d", i);
993                 do_lcfg(MDCDEV, 0, LCFG_DEL_UUID, niduuid, 0, 0, 0);
994         }
995         /* class_import_put will get rid of the additional connections */
996 out:
997         RETURN(rc);
998 }
999 /* end COMPAT_146 */
1000
1001 #ifdef HAVE_BDI_REGISTER
1002 static atomic_t ll_bdi_num = ATOMIC_INIT(0);
1003 #endif
1004
1005 int ll_fill_super(struct super_block *sb)
1006 {
1007         struct lustre_profile *lprof = NULL;
1008         struct lustre_sb_info *lsi = s2lsi(sb);
1009         struct ll_sb_info *sbi;
1010         char  *osc = NULL, *mdc = NULL;
1011         char  *profilenm = get_profile_name(sb);
1012         struct config_llog_instance cfg = {0, };
1013         char   ll_instance[sizeof(sb) * 2 + 1];
1014         int    err;
1015         char  *save = NULL;
1016         char  pseudo[32] = { 0 };
1017         ENTRY;
1018
1019         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
1020
1021         cfs_module_get();
1022
1023         /* client additional sb info */
1024         lsi->lsi_llsbi = sbi = ll_init_sbi();
1025         if (!sbi) {
1026                 cfs_module_put();
1027                 RETURN(-ENOMEM);
1028         }
1029
1030         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
1031         if (err)
1032                 GOTO(out_free, err);
1033
1034         /* Generate a string unique to this super, in case some joker tries
1035            to mount the same fs at two mount points.
1036            Use the address of the super itself.*/
1037         sprintf(ll_instance, "%p", sb);
1038         cfg.cfg_instance = ll_instance;
1039         cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1040         cfg.cfg_sb = sb;
1041
1042         /* set up client obds */
1043         if (strchr(profilenm, '/') != NULL) /* COMPAT_146 */
1044                 err = -EINVAL; /* skip error messages, use old config code */
1045         else
1046                 err = lustre_process_log(sb, profilenm, &cfg);
1047         /* COMPAT_146 */
1048         if (err < 0) {
1049                 char *oldname;
1050                 int rc, oldnamelen;
1051                 oldnamelen = strlen(profilenm) + 1;
1052                 /* Temp storage for 1.4.6 profile name */
1053                 OBD_ALLOC(oldname, oldnamelen);
1054                 if (oldname) {
1055                         memcpy(oldname, profilenm, oldnamelen);
1056                         rc = old_lustre_process_log(sb, oldname, &cfg);
1057                         if (rc >= 0) {
1058                                 /* That worked - update the profile name
1059                                    permanently */
1060                                 err = rc;
1061                                 OBD_FREE(lsi->lsi_lmd->lmd_profile,
1062                                          strlen(lsi->lsi_lmd->lmd_profile) + 1);
1063                                 OBD_ALLOC(lsi->lsi_lmd->lmd_profile,
1064                                          strlen(oldname) + 1);
1065                                 if (!lsi->lsi_lmd->lmd_profile) {
1066                                         OBD_FREE(oldname, oldnamelen);
1067                                         GOTO(out_free, err = -ENOMEM);
1068                                 }
1069                                 memcpy(lsi->lsi_lmd->lmd_profile, oldname,
1070                                        strlen(oldname) + 1);
1071                                 profilenm = get_profile_name(sb);
1072                                 /* Don't ever try to recover the MGS */
1073                                 rc = ptlrpc_set_import_active(
1074                                         lsi->lsi_mgc->u.cli.cl_import, 0);
1075                         }
1076                         OBD_FREE(oldname, oldnamelen);
1077                 }
1078         }
1079         /* end COMPAT_146 */
1080         if (err < 0) {
1081                 CERROR("Unable to process log: %d\n", err);
1082                 GOTO(out_free, err);
1083         }
1084
1085         lprof = class_get_profile(profilenm);
1086         if (lprof == NULL) {
1087                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1088                                    " read from the MGS.  Does that filesystem "
1089                                    "exist?\n", profilenm);
1090                 GOTO(out_free, err = -EINVAL);
1091         }
1092
1093         /*
1094          * The configuration for 1.8 client and 2.0 client are different.
1095          * 2.0 introduces lmv, but 1.8 directly uses mdc.
1096          * Here, we will hack to use proper name for mdc if needed.
1097          */
1098         {
1099                 char *fsname_end;
1100                 int namelen;
1101
1102                 save = lprof->lp_mdc;
1103                 fsname_end = strrchr(save, '-');
1104                 if (fsname_end) {
1105                         namelen = fsname_end - save;
1106                         if (strcmp(fsname_end, "-clilmv") == 0) {
1107                                 strncpy(pseudo, save, namelen);
1108                                 strcat(pseudo, "-MDT0000-mdc");
1109                                 lprof->lp_mdc = pseudo;
1110                                 CDEBUG(D_INFO, "1.8.x connecting to 2.0: lmv=%s"
1111                                        " new mdc=%s\n", save, pseudo);
1112                         }
1113                 }
1114         }
1115
1116         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1117                lprof->lp_mdc, lprof->lp_osc);
1118
1119         OBD_ALLOC(osc, strlen(lprof->lp_osc) +
1120                   strlen(ll_instance) + 2);
1121         if (!osc)
1122                 GOTO(out_free, err = -ENOMEM);
1123         sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
1124
1125         OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
1126                   strlen(ll_instance) + 2);
1127         if (!mdc)
1128                 GOTO(out_free, err = -ENOMEM);
1129         sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
1130
1131         /* connections, registrations, sb setup */
1132         err = client_common_fill_super(sb, mdc, osc);
1133         if (err)
1134                 GOTO(out_free, err);
1135
1136 #ifdef HAVE_BDI_INIT
1137         err = bdi_init(&lsi->lsi_bdi);
1138         if (err)
1139                 GOTO(out_free, err);
1140         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1141         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1142 #ifdef HAVE_BDI_REGISTER
1143         err = bdi_register(&lsi->lsi_bdi, NULL, "lustre-%d",
1144                            atomic_inc_return(&ll_bdi_num));
1145 #endif
1146 #endif
1147
1148 #ifdef HAVE_SB_BDI
1149         sb->s_bdi    = &lsi->lsi_bdi;
1150 #endif
1151
1152 out_free:
1153         if (save && lprof)
1154                 lprof->lp_mdc = save;
1155         if (mdc)
1156                 OBD_FREE(mdc, strlen(mdc) + 1);
1157         if (osc)
1158                 OBD_FREE(osc, strlen(osc) + 1);
1159         if (err)
1160                 ll_put_super(sb);
1161         else
1162                 LCONSOLE_WARN("Client %s has started\n", profilenm);
1163
1164         RETURN(err);
1165 } /* ll_fill_super */
1166
1167
1168 void ll_put_super(struct super_block *sb)
1169 {
1170         struct config_llog_instance cfg;
1171         char   ll_instance[sizeof(sb) * 2 + 1];
1172         struct obd_device *obd;
1173         struct lustre_sb_info *lsi = s2lsi(sb);
1174         struct ll_sb_info *sbi = ll_s2sbi(sb);
1175         char *profilenm = get_profile_name(sb);
1176         int force = 1, next;
1177         ENTRY;
1178
1179         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
1180
1181         sprintf(ll_instance, "%p", sb);
1182         cfg.cfg_instance = ll_instance;
1183         lustre_end_log(sb, NULL, &cfg);
1184
1185         if (sbi->ll_mdc_exp) {
1186                 obd = class_exp2obd(sbi->ll_mdc_exp);
1187                 if (obd)
1188                         force = obd->obd_force;
1189         }
1190
1191         /* We need to set force before the lov_disconnect in
1192            lustre_common_put_super, since l_d cleans up osc's as well. */
1193         if (force) {
1194                 next = 0;
1195                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1196                                                      &next)) != NULL) {
1197                         obd->obd_force = force;
1198                 }
1199         }
1200
1201         if (sbi->ll_lcq) {
1202                 /* Only if client_common_fill_super succeeded */
1203                 client_common_put_super(sb);
1204         }
1205
1206         next = 0;
1207         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1208                 class_manual_cleanup(obd);
1209         }
1210
1211         if (profilenm)
1212                 class_del_profile(profilenm);
1213
1214 #ifdef HAVE_BDI_INIT
1215         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1216                 bdi_destroy(&lsi->lsi_bdi);
1217                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1218         }
1219 #endif
1220
1221         ll_free_sbi(sb);
1222         lsi->lsi_llsbi = NULL;
1223
1224         lustre_common_put_super(sb);
1225
1226         LCONSOLE_WARN("client %s umount complete\n", ll_instance);
1227
1228         cfs_module_put();
1229
1230         EXIT;
1231 } /* client_put_super */
1232
1233 int ll_cache_shrink(SHRINKER_FIRST_ARG int nr_to_scan,
1234                            gfp_t gfp_mask)
1235 {
1236         struct ll_sb_info *sbi;
1237         int count = 0;
1238
1239         if ((nr_to_scan != 0) && !(gfp_mask & __GFP_FS))
1240                 return -1;
1241
1242         /* don't race with umount */
1243         down_read(&ll_sb_sem);
1244         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
1245                 count += llap_shrink_cache(sbi, nr_to_scan);
1246         up_read(&ll_sb_sem);
1247
1248         return count;
1249 }
1250
1251 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1252 {
1253         struct inode *inode = NULL;
1254         /* NOTE: we depend on atomic igrab() -bzzz */
1255         lock_res_and_lock(lock);
1256         if (lock->l_ast_data) {
1257                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1258                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1259                         inode = igrab(lock->l_ast_data);
1260                 } else {
1261                         inode = lock->l_ast_data;
1262                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1263                                          D_WARNING, lock, "l_ast_data %p is "
1264                                          "bogus: magic %08x", lock->l_ast_data,
1265                                          lli->lli_inode_magic);
1266                         inode = NULL;
1267                 }
1268         }
1269         unlock_res_and_lock(lock);
1270         return inode;
1271 }
1272
1273 static int null_if_equal(struct ldlm_lock *lock, void *data)
1274 {
1275         if (data == lock->l_ast_data) {
1276                 lock->l_ast_data = NULL;
1277
1278                 if (lock->l_req_mode != lock->l_granted_mode)
1279                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
1280         }
1281
1282         return LDLM_ITER_CONTINUE;
1283 }
1284
1285 void ll_clear_inode(struct inode *inode)
1286 {
1287         struct ll_fid fid;
1288         struct ll_inode_info *lli = ll_i2info(inode);
1289         struct ll_sb_info *sbi = ll_i2sbi(inode);
1290         ENTRY;
1291
1292         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1293                inode->i_generation, inode);
1294
1295         if (S_ISDIR(inode->i_mode)) {
1296                 /* these should have been cleared in ll_file_release */
1297                 LASSERT(lli->lli_sai == NULL);
1298                 LASSERT(lli->lli_opendir_key == NULL);
1299                 LASSERT(lli->lli_opendir_pid == 0);
1300         }
1301
1302         ll_inode2fid(&fid, inode);
1303         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1304         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
1305
1306         LASSERT(!lli->lli_open_fd_write_count);
1307         LASSERT(!lli->lli_open_fd_read_count);
1308         LASSERT(!lli->lli_open_fd_exec_count);
1309
1310         if (lli->lli_mds_write_och)
1311                 ll_mdc_real_close(inode, FMODE_WRITE);
1312         if (lli->lli_mds_exec_och) {
1313                 if (!FMODE_EXEC)
1314                         CERROR("No FMODE exec, bug exec och is present for "
1315                                "inode %ld\n", inode->i_ino);
1316                 ll_mdc_real_close(inode, FMODE_EXEC);
1317         }
1318         if (lli->lli_mds_read_och)
1319                 ll_mdc_real_close(inode, FMODE_READ);
1320
1321
1322         if (lli->lli_smd) {
1323                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
1324                                   null_if_equal, inode);
1325
1326                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
1327                 lli->lli_smd = NULL;
1328         }
1329
1330         if (lli->lli_symlink_name) {
1331                 OBD_FREE(lli->lli_symlink_name,
1332                          strlen(lli->lli_symlink_name) + 1);
1333                 lli->lli_symlink_name = NULL;
1334         }
1335
1336 #ifdef CONFIG_FS_POSIX_ACL
1337         if (lli->lli_posix_acl) {
1338                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1339                 posix_acl_release(lli->lli_posix_acl);
1340                 lli->lli_posix_acl = NULL;
1341         }
1342 #endif
1343
1344         lli->lli_inode_magic = LLI_INODE_DEAD;
1345
1346         EXIT;
1347 }
1348 static int ll_setattr_do_truncate(struct inode *inode, loff_t new_size)
1349 {
1350         struct ll_sb_info *sbi = ll_i2sbi(inode);
1351         struct ll_inode_info *lli = ll_i2info(inode);
1352         struct lov_stripe_md *lsm = lli->lli_smd;
1353         int rc;
1354         ldlm_policy_data_t policy = { .l_extent = {new_size,
1355                                                    OBD_OBJECT_EOF } };
1356         struct lustre_handle lockh = { 0 };
1357         int local_lock = 1; /* 0 - no local lock;
1358                              * 1 - lock taken by lock_extent;
1359                              * 2 - by obd_match*/
1360         int ast_flags;
1361         ENTRY;
1362
1363         UNLOCK_INODE_MUTEX(inode);
1364         UP_WRITE_I_ALLOC_SEM(inode);
1365
1366         down_write(&lli->lli_truncate_rwsem);
1367         if (sbi->ll_lockless_truncate_enable &&
1368             (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK)) {
1369                 int n_matches = 0;
1370
1371                 ast_flags = LDLM_FL_BLOCK_GRANTED;
1372                 rc = obd_match(sbi->ll_osc_exp, lsm, LDLM_EXTENT,
1373                                &policy, LCK_PW, &ast_flags, inode, &lockh,
1374                                &n_matches);
1375                 if (rc > 0) {
1376                         local_lock = 2;
1377                         rc = 0;
1378                 } else {
1379                         /* clear the lock handle as it not cleared
1380                          * by obd_match if no matched lock found */
1381                         lockh.cookie = 0;
1382                         if (rc == 0 && n_matches == 0) {
1383                                 local_lock = 0;
1384                                 rc = ll_file_punch(inode, new_size, 1);
1385                         }
1386                 }
1387         }
1388         if (local_lock == 1) {
1389                 /* XXX when we fix the AST intents to pass the discard-range
1390                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1391                  * XXX here. */
1392                 ast_flags = (new_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
1393                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy,
1394                                     &lockh, ast_flags);
1395                 if (unlikely(rc != 0))
1396                         local_lock = 0;
1397         }
1398
1399         LOCK_INODE_MUTEX(inode);
1400         DOWN_WRITE_I_ALLOC_SEM(inode);
1401         if (likely(rc == 0)) {
1402                 /* Only ll_inode_size_lock is taken at this level.
1403                  * lov_stripe_lock() is grabbed by ll_truncate() only over
1404                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
1405                  * ll_truncate dropped ll_inode_size_lock() */
1406                 ll_inode_size_lock(inode, 0);
1407                 if (!local_lock)
1408                         set_bit(LLI_F_SRVLOCK, &lli->lli_flags);
1409                 rc = vmtruncate(inode, new_size);
1410                 clear_bit(LLI_F_SRVLOCK, &lli->lli_flags);
1411                 if (rc != 0) {
1412                         LASSERT(SEM_COUNT(&lli->lli_size_sem) <= 0);
1413                         ll_inode_size_unlock(inode, 0);
1414                 }
1415         }
1416         if (local_lock) {
1417                 int err;
1418
1419                 err = (local_lock == 2) ?
1420                         obd_cancel(sbi->ll_osc_exp, lsm, LCK_PW, &lockh, 0, 0):
1421                         ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1422                 if (unlikely(err != 0)){
1423                         CERROR("extent unlock failed: err=%d,"
1424                                " unlock method =%d\n", err, local_lock);
1425                         if (rc == 0)
1426                                 rc = err;
1427                 }
1428         }
1429         up_write(&lli->lli_truncate_rwsem);
1430         RETURN(rc);
1431 }
1432
1433 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1434  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1435  * keep these values until such a time that objects are allocated for it.
1436  * We do the MDS operations first, as it is checking permissions for us.
1437  * We don't to the MDS RPC if there is nothing that we want to store there,
1438  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1439  * going to do an RPC anyways.
1440  *
1441  * If we are doing a truncate, we will send the mtime and ctime updates
1442  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1443  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1444  * at the same time.
1445  */
1446 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1447 {
1448         struct ll_inode_info *lli = ll_i2info(inode);
1449         struct lov_stripe_md *lsm = lli->lli_smd;
1450         struct ll_sb_info *sbi = ll_i2sbi(inode);
1451         struct ptlrpc_request *request = NULL;
1452         struct mdc_op_data *op_data;
1453         struct lustre_md md;
1454         int ia_valid = attr->ia_valid;
1455         int rc = 0;
1456         ENTRY;
1457
1458         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
1459                attr->ia_valid);
1460         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETATTR, 1);
1461
1462         if (ia_valid & ATTR_SIZE) {
1463                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1464                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1465                                attr->ia_size, ll_file_maxbytes(inode));
1466                         RETURN(-EFBIG);
1467                 }
1468
1469                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1470         }
1471
1472         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1473         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1474                 if (cfs_curproc_fsuid() != inode->i_uid &&
1475                     !cfs_capable(CFS_CAP_FOWNER))
1476                         RETURN(-EPERM);
1477         }
1478
1479         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1480         if (attr->ia_valid & ATTR_CTIME) {
1481                 attr->ia_ctime = CURRENT_TIME;
1482                 attr->ia_valid |= ATTR_CTIME_SET;
1483         }
1484         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1485                 attr->ia_atime = CURRENT_TIME;
1486                 attr->ia_valid |= ATTR_ATIME_SET;
1487         }
1488         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1489                 attr->ia_mtime = CURRENT_TIME;
1490                 attr->ia_valid |= ATTR_MTIME_SET;
1491         }
1492
1493         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1494                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1495                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1496                        CURRENT_SECONDS);
1497
1498         /* NB: ATTR_SIZE will only be set after this point if the size
1499          * resides on the MDS, ie, this file has no objects. */
1500         if (lsm)
1501                 attr->ia_valid &= ~ATTR_SIZE;
1502
1503         OBD_ALLOC_PTR(op_data);
1504         if (NULL == op_data)
1505                 RETURN(-ENOMEM);
1506         /* We always do an MDS RPC, even if we're only changing the size;
1507          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1508         ll_prepare_mdc_op_data(op_data, inode, NULL, NULL, 0, 0, NULL);
1509
1510         rc = mdc_setattr(sbi->ll_mdc_exp, op_data,
1511                          attr, NULL, 0, NULL, 0, &request);
1512
1513         if (rc) {
1514                 ptlrpc_req_finished(request);
1515                 if (rc == -ENOENT) {
1516                         inode->i_nlink = 0;
1517                         /* Unlinked special device node?  Or just a race?
1518                          * Pretend we done everything. */
1519                         if (!S_ISREG(inode->i_mode) &&
1520                             !S_ISDIR(inode->i_mode))
1521                                 rc = inode_setattr(inode, attr);
1522                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY)
1523                         CERROR("mdc_setattr fails: rc = %d\n", rc);
1524                 OBD_FREE_PTR(op_data);
1525                 RETURN(rc);
1526         }
1527         OBD_FREE_PTR(op_data);
1528
1529         rc = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
1530         if (rc) {
1531                 ptlrpc_req_finished(request);
1532                 RETURN(rc);
1533         }
1534
1535         /* We call inode_setattr to adjust timestamps.
1536          * If there is at least some data in file, we cleared ATTR_SIZE above to
1537          * avoid invoking vmtruncate, otherwise it is important to call
1538          * vmtruncate in inode_setattr to update inode->i_size (bug 6196) */
1539         rc = inode_setattr(inode, attr);
1540
1541         ll_update_inode(inode, &md);
1542         ptlrpc_req_finished(request);
1543
1544         if (!lsm || !S_ISREG(inode->i_mode)) {
1545                 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1546                 RETURN(rc);
1547         }
1548
1549         /* We really need to get our PW lock before we change inode->i_size.
1550          * If we don't we can race with other i_size updaters on our node, like
1551          * ll_file_read.  We can also race with i_size propogation to other
1552          * nodes through dirtying and writeback of final cached pages.  This
1553          * last one is especially bad for racing o_append users on other
1554          * nodes. */
1555         if (ia_valid & ATTR_SIZE) {
1556                 rc = ll_setattr_do_truncate(inode, attr->ia_size);
1557         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1558                 struct obd_info *oinfo;
1559                 struct obdo *oa;
1560                 struct lustre_handle lockh = { 0 };
1561                 obd_valid valid;
1562
1563                 OBD_ALLOC_PTR(oinfo);
1564                 if (NULL == oinfo)
1565                         RETURN(-ENOMEM);
1566
1567                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1568                        inode->i_ino, LTIME_S(attr->ia_mtime));
1569
1570                 OBDO_ALLOC(oa);
1571                 if (oa) {
1572                         oa->o_id = lsm->lsm_object_id;
1573                         oa->o_gr = lsm->lsm_object_gr;
1574                         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1575
1576                         valid = OBD_MD_FLTYPE | OBD_MD_FLFID | OBD_MD_FLGENER;
1577
1578                         if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
1579                                 struct ost_lvb xtimes;
1580
1581                                 UNLOCK_INODE_MUTEX(inode);
1582                                 /* setting mtime to past is performed under PW
1583                                  * EOF extent lock */
1584                                 oinfo->oi_policy.l_extent.start = 0;
1585                                 oinfo->oi_policy.l_extent.end = OBD_OBJECT_EOF;
1586                                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW,
1587                                                     &oinfo->oi_policy,
1588                                                     &lockh, 0);
1589                                 LOCK_INODE_MUTEX(inode);
1590                                 if (rc) {
1591                                         OBDO_FREE(oa);
1592                                         OBD_FREE_PTR(oinfo);
1593                                         RETURN(rc);
1594                                 }
1595
1596                                 /* setattr under locks
1597                                  *
1598                                  * 1. restore inode's timestamps which
1599                                  * are about to be set as long as
1600                                  * concurrent stat (via
1601                                  * ll_glimpse_size) might bring
1602                                  * out-of-date ones
1603                                  *
1604                                  * 2. update lsm so that next stat
1605                                  * (via ll_glimpse_size) could get
1606                                  * correct values in lsm */
1607                                 lov_stripe_lock(lli->lli_smd);
1608                                 if (ia_valid & ATTR_ATIME) {
1609                                         LTIME_S(inode->i_atime) =
1610                                                 xtimes.lvb_atime =
1611                                                 LTIME_S(attr->ia_atime);
1612                                         valid |= OBD_MD_FLATIME;
1613                                 }
1614                                 if (ia_valid & ATTR_MTIME) {
1615                                         LTIME_S(inode->i_mtime) =
1616                                                 xtimes.lvb_mtime =
1617                                                 LTIME_S(attr->ia_mtime);
1618                                         valid |= OBD_MD_FLMTIME;
1619                                 }
1620                                 if (ia_valid & ATTR_CTIME) {
1621                                         LTIME_S(inode->i_ctime) =
1622                                                 xtimes.lvb_ctime =
1623                                                 LTIME_S(attr->ia_ctime);
1624                                         valid |= OBD_MD_FLCTIME;
1625                                 }
1626
1627                                 obd_update_lvb(ll_i2obdexp(inode), lli->lli_smd,
1628                                                &xtimes, valid);
1629                                 lov_stripe_unlock(lli->lli_smd);
1630                         } else {
1631                                 /* lockless setattr
1632                                  *
1633                                  * 1. do not use inode's timestamps
1634                                  * because concurrent stat might fill
1635                                  * the inode with out-of-date times,
1636                                  * send values from attr instead
1637                                  *
1638                                  * 2.do no update lsm, as long as stat
1639                                  * (via ll_glimpse_size) will bring
1640                                  * attributes from osts anyway */
1641                                 if (ia_valid & ATTR_ATIME) {
1642                                         oa->o_atime = LTIME_S(attr->ia_atime);
1643                                         oa->o_valid |= OBD_MD_FLATIME;
1644                                 }
1645                                 if (ia_valid & ATTR_MTIME) {
1646                                         oa->o_mtime = LTIME_S(attr->ia_mtime);
1647                                         oa->o_valid |= OBD_MD_FLMTIME;
1648                                 }
1649                                 if (ia_valid & ATTR_CTIME) {
1650                                         oa->o_ctime = LTIME_S(attr->ia_ctime);
1651                                         oa->o_valid |= OBD_MD_FLCTIME;
1652                                 }
1653                         }
1654
1655                         obdo_from_inode(oa, inode, valid);
1656
1657                         oinfo->oi_oa = oa;
1658                         oinfo->oi_md = lsm;
1659
1660                         rc = obd_setattr_rqset(sbi->ll_osc_exp, oinfo, NULL);
1661                         if (rc)
1662                                 CERROR("obd_setattr_async fails: rc=%d\n", rc);
1663
1664                         if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
1665                                 int err;
1666
1667                                 err = ll_extent_unlock(NULL, inode, lsm,
1668                                                        LCK_PW, &lockh);
1669                                 if (unlikely(err != 0)) {
1670                                         CERROR("extent unlock failed: "
1671                                                "err=%d\n", err);
1672                                         if (rc == 0)
1673                                                 rc = err;
1674                                 }
1675                         }
1676                         OBDO_FREE(oa);
1677                 } else {
1678                         rc = -ENOMEM;
1679                 }
1680                 OBD_FREE_PTR(oinfo);
1681         }
1682         RETURN(rc);
1683 }
1684
1685 int ll_setattr(struct dentry *de, struct iattr *attr)
1686 {
1687         int mode = de->d_inode->i_mode;
1688
1689         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1690             (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1691                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1692         if ((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1693             (ATTR_SIZE|ATTR_MODE)) {
1694                 if (((mode & S_ISUID) && (!(attr->ia_mode & S_ISUID))) ||
1695                     ((mode & S_ISGID) && (mode & S_IXGRP) &&
1696                     (!(attr->ia_mode & S_ISGID))))
1697                         attr->ia_valid |= ATTR_FORCE;
1698         }
1699
1700         if ((mode & S_ISUID) && S_ISREG(mode) &&
1701             !(attr->ia_mode & S_ISUID))
1702                 attr->ia_valid |= ATTR_KILL_SUID;
1703
1704         if (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1705             S_ISREG(mode) && !(attr->ia_mode & S_ISGID))
1706                 attr->ia_valid |= ATTR_KILL_SGID;
1707
1708         return ll_setattr_raw(de->d_inode, attr);
1709 }
1710
1711 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1712                        __u64 max_age, __u32 flags)
1713 {
1714         struct ll_sb_info *sbi = ll_s2sbi(sb);
1715         struct obd_statfs obd_osfs;
1716         int rc;
1717         ENTRY;
1718
1719         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age, flags);
1720         if (rc) {
1721                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1722                 RETURN(rc);
1723         }
1724
1725         osfs->os_type = sb->s_magic;
1726
1727         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1728                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1729
1730         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1731                 flags |= OBD_STATFS_NODELAY;
1732
1733         rc = obd_statfs_rqset(class_exp2obd(sbi->ll_osc_exp),
1734                               &obd_osfs, max_age, flags);
1735         if (rc) {
1736                 CERROR("obd_statfs fails: rc = %d\n", rc);
1737                 RETURN(rc);
1738         }
1739
1740         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1741                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1742                obd_osfs.os_files);
1743
1744         osfs->os_bsize = obd_osfs.os_bsize;
1745         osfs->os_blocks = obd_osfs.os_blocks;
1746         osfs->os_bfree = obd_osfs.os_bfree;
1747         osfs->os_bavail = obd_osfs.os_bavail;
1748
1749         /* If we don't have as many objects free on the OST as inodes
1750          * on the MDS, we reduce the total number of inodes to
1751          * compensate, so that the "inodes in use" number is correct.
1752          */
1753         if (obd_osfs.os_ffree < osfs->os_ffree) {
1754                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1755                         obd_osfs.os_ffree;
1756                 osfs->os_ffree = obd_osfs.os_ffree;
1757         }
1758
1759         RETURN(rc);
1760 }
1761 #ifndef HAVE_STATFS_DENTRY_PARAM
1762 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1763 {
1764 #else
1765 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1766 {
1767         struct super_block *sb = de->d_sb;
1768 #endif
1769         struct obd_statfs osfs;
1770         int rc;
1771
1772         CDEBUG(D_VFSTRACE, "VFS Op: at "LPU64" jiffies\n", get_jiffies_64());
1773         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1774
1775         /* Some amount of caching on the client is allowed */
1776         rc = ll_statfs_internal(sb, &osfs,
1777                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1778                                 0);
1779         if (rc)
1780                 return rc;
1781
1782         statfs_unpack(sfs, &osfs);
1783
1784         /* We need to downshift for all 32-bit kernels, because we can't
1785          * tell if the kernel is being called via sys_statfs64() or not.
1786          * Stop before overflowing f_bsize - in which case it is better
1787          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1788         if (sizeof(long) < 8) {
1789                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1790                         sfs->f_bsize <<= 1;
1791
1792                         osfs.os_blocks >>= 1;
1793                         osfs.os_bfree >>= 1;
1794                         osfs.os_bavail >>= 1;
1795                 }
1796         }
1797
1798         sfs->f_blocks = osfs.os_blocks;
1799         sfs->f_bfree = osfs.os_bfree;
1800         sfs->f_bavail = osfs.os_bavail;
1801
1802         return 0;
1803 }
1804
1805 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
1806 {
1807         struct ll_inode_info *lli;
1808         struct lov_stripe_md *lsm;
1809
1810         lli = ll_i2info(inode);
1811         LASSERT(lli->lli_size_sem_owner != current);
1812         down(&lli->lli_size_sem);
1813         LASSERT(lli->lli_size_sem_owner == NULL);
1814         lli->lli_size_sem_owner = current;
1815         lsm = lli->lli_smd;
1816         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
1817                  lsm, lock_lsm);
1818         if (lock_lsm)
1819                 lov_stripe_lock(lsm);
1820 }
1821
1822 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1823 {
1824         struct ll_inode_info *lli;
1825         struct lov_stripe_md *lsm;
1826
1827         lli = ll_i2info(inode);
1828         lsm = lli->lli_smd;
1829         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1830                  lsm, unlock_lsm);
1831         if (unlock_lsm)
1832                 lov_stripe_unlock(lsm);
1833         LASSERT(lli->lli_size_sem_owner == current);
1834         lli->lli_size_sem_owner = NULL;
1835         up(&lli->lli_size_sem);
1836 }
1837
1838 static void ll_replace_lsm(struct inode *inode, struct lov_stripe_md *lsm)
1839 {
1840         struct ll_inode_info *lli = ll_i2info(inode);
1841
1842         dump_lsm(D_INODE, lsm);
1843         dump_lsm(D_INODE, lli->lli_smd);
1844         LASSERTF(lsm->lsm_magic == LOV_MAGIC_JOIN,
1845                  "lsm must be joined lsm %p\n", lsm);
1846         obd_free_memmd(ll_i2obdexp(inode), &lli->lli_smd);
1847         CDEBUG(D_INODE, "replace lsm %p to lli_smd %p for inode %lu%u(%p)\n",
1848                lsm, lli->lli_smd, inode->i_ino, inode->i_generation, inode);
1849         lli->lli_smd = lsm;
1850         lli->lli_maxbytes = lsm->lsm_maxbytes;
1851         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1852                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1853 }
1854
1855 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1856 {
1857         struct ll_inode_info *lli = ll_i2info(inode);
1858         struct mds_body *body = md->body;
1859         struct lov_stripe_md *lsm = md->lsm;
1860         struct ll_sb_info *sbi = ll_i2sbi(inode);
1861         ENTRY;
1862
1863         CDEBUG(D_INODE, "body->valid = "LPX64"\n", body->valid);
1864
1865         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1866         if (lsm != NULL) {
1867                 spin_lock(&lli->lli_lock);
1868                 if (lli->lli_smd == NULL) {
1869                         if (lsm->lsm_magic != LOV_MAGIC_V1 &&
1870                             lsm->lsm_magic != LOV_MAGIC_V3 &&
1871                             lsm->lsm_magic != LOV_MAGIC_JOIN) {
1872                                 dump_lsm(D_ERROR, lsm);
1873                                 LBUG();
1874                         }
1875                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1876                                lsm, inode->i_ino, inode->i_generation, inode);
1877                         /* ll_inode_size_lock() requires it is only called
1878                          * with lli_smd != NULL or lock_lsm == 0 or we can
1879                          * race between lock/unlock.  bug 9547 */
1880                         lli->lli_smd = lsm;
1881                         spin_unlock(&lli->lli_lock);
1882                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1883                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1884                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1885                 } else {
1886                         spin_unlock(&lli->lli_lock);
1887                         if ((lli->lli_smd->lsm_magic == lsm->lsm_magic ||
1888                              (lli->lli_smd->lsm_magic == LOV_MAGIC_V3 &&
1889                               lsm->lsm_magic == LOV_MAGIC_V1) ||
1890                              (lli->lli_smd->lsm_magic == LOV_MAGIC_V1 &&
1891                               lsm->lsm_magic == LOV_MAGIC_V3)) &&
1892                             lli->lli_smd->lsm_stripe_count ==
1893                                         lsm->lsm_stripe_count) {
1894                                 /* The MDS can suddenly change the magic to
1895                                  * v1/v3 if it has been upgraded/downgraded to a
1896                                  * version that does/doesn't support OST pools.
1897                                  * In this case, we consider that the cached lsm
1898                                  * is equivalent to the new one and keep it in
1899                                  * place until the inode is evicted from the
1900                                  * cache */
1901                                 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1902                                         CERROR("lsm mismatch for inode %ld\n",
1903                                                 inode->i_ino);
1904                                         CERROR("lli_smd:\n");
1905                                         dump_lsm(D_ERROR, lli->lli_smd);
1906                                         CERROR("lsm:\n");
1907                                         dump_lsm(D_ERROR, lsm);
1908                                         LBUG();
1909                                 }
1910                         } else
1911                                 ll_replace_lsm(inode, lsm);
1912                 }
1913                 if (lli->lli_smd != lsm)
1914                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1915         }
1916
1917 #ifdef CONFIG_FS_POSIX_ACL
1918         LASSERT(!md->posix_acl || (body->valid & OBD_MD_FLACL));
1919         if (body->valid & OBD_MD_FLACL) {
1920                 spin_lock(&lli->lli_lock);
1921                 if (lli->lli_posix_acl)
1922                         posix_acl_release(lli->lli_posix_acl);
1923                 lli->lli_posix_acl = md->posix_acl;
1924                 spin_unlock(&lli->lli_lock);
1925         }
1926 #endif
1927
1928         inode->i_ino = ll_fid_build_ino(&body->fid1);
1929         inode->i_generation = ll_fid_build_gen(sbi, &body->fid1);
1930         *ll_inode_lu_fid(inode) = *((struct lu_fid*)&md->body->fid1);
1931
1932         if (body->valid & OBD_MD_FLATIME) {
1933                 if (body->atime > LTIME_S(inode->i_atime))
1934                         LTIME_S(inode->i_atime) = body->atime;
1935                 lli->lli_lvb.lvb_atime = body->atime;
1936         }
1937         if (body->valid & OBD_MD_FLMTIME) {
1938                 if (body->mtime > LTIME_S(inode->i_mtime)) {
1939                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
1940                                "to "LPU64"\n", inode->i_ino,
1941                                LTIME_S(inode->i_mtime), body->mtime);
1942                         LTIME_S(inode->i_mtime) = body->mtime;
1943                 }
1944                 lli->lli_lvb.lvb_mtime = body->mtime;
1945         }
1946         if (body->valid & OBD_MD_FLCTIME) {
1947                 if (body->ctime > LTIME_S(inode->i_ctime))
1948                         LTIME_S(inode->i_ctime) = body->ctime;
1949                 lli->lli_lvb.lvb_ctime = body->ctime;
1950         }
1951         if (body->valid & OBD_MD_FLMODE)
1952                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1953         if (body->valid & OBD_MD_FLTYPE)
1954                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1955         if (S_ISREG(inode->i_mode)) {
1956                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS+1, LL_MAX_BLKSIZE_BITS);
1957         } else {
1958                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1959         }
1960 #ifdef HAVE_INODE_BLKSIZE
1961         inode->i_blksize = 1<<inode->i_blkbits;
1962 #endif
1963         if (body->valid & OBD_MD_FLUID)
1964                 inode->i_uid = body->uid;
1965         if (body->valid & OBD_MD_FLGID)
1966                 inode->i_gid = body->gid;
1967         if (body->valid & OBD_MD_FLFLAGS)
1968                 inode->i_flags = ll_ext_to_inode_flags(body->flags |
1969                                                        MDS_BFLAG_EXT_FLAGS);
1970         if (body->valid & OBD_MD_FLNLINK)
1971                 inode->i_nlink = body->nlink;
1972
1973         if (body->valid & OBD_MD_FLRDEV)
1974                 inode->i_rdev = old_decode_dev(body->rdev);
1975         if (body->valid & OBD_MD_FLSIZE) {
1976 #if 0           /* Can't block ll_test_inode->ll_update_inode, b=14326*/
1977                 ll_inode_size_lock(inode, 0);
1978                 i_size_write(inode, body->size);
1979                 ll_inode_size_unlock(inode, 0);
1980 #else
1981                 inode->i_size = body->size;
1982 #endif
1983         }
1984         if (body->valid & OBD_MD_FLBLOCKS)
1985                 inode->i_blocks = body->blocks;
1986
1987         if (body->valid & OBD_MD_FLSIZE)
1988                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1989         EXIT;
1990 }
1991
1992 void ll_read_inode2(struct inode *inode, void *opaque)
1993 {
1994         struct lustre_md *md = opaque;
1995         struct ll_inode_info *lli = ll_i2info(inode);
1996         ENTRY;
1997
1998         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1999                inode->i_generation, inode);
2000
2001         ll_lli_init(lli);
2002
2003         LASSERT(!lli->lli_smd);
2004
2005         /* Core attributes from the MDS first.  This is a new inode, and
2006          * the VFS doesn't zero times in the core inode so we have to do
2007          * it ourselves.  They will be overwritten by either MDS or OST
2008          * attributes - we just need to make sure they aren't newer. */
2009         LTIME_S(inode->i_mtime) = 0;
2010         LTIME_S(inode->i_atime) = 0;
2011         LTIME_S(inode->i_ctime) = 0;
2012         inode->i_rdev = 0;
2013         ll_update_inode(inode, md);
2014
2015         /* OIDEBUG(inode); */
2016
2017         /* initializing backing dev info. */
2018         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
2019
2020         if (S_ISREG(inode->i_mode)) {
2021                 struct ll_sb_info *sbi = ll_i2sbi(inode);
2022                 inode->i_op = &ll_file_inode_operations;
2023                 inode->i_fop = sbi->ll_fop;
2024                 inode->i_mapping->a_ops = &ll_aops;
2025                 EXIT;
2026         } else if (S_ISDIR(inode->i_mode)) {
2027                 inode->i_op = &ll_dir_inode_operations;
2028                 inode->i_fop = &ll_dir_operations;
2029                 inode->i_mapping->a_ops = &ll_dir_aops;
2030                 EXIT;
2031         } else if (S_ISLNK(inode->i_mode)) {
2032                 inode->i_op = &ll_fast_symlink_inode_operations;
2033                 EXIT;
2034         } else {
2035                 inode->i_op = &ll_special_inode_operations;
2036                 init_special_inode(inode, inode->i_mode,
2037                                    kdev_t_to_nr(inode->i_rdev));
2038                 EXIT;
2039         }
2040 }
2041
2042 int ll_iocontrol(struct inode *inode, struct file *file,
2043                  unsigned int cmd, unsigned long arg)
2044 {
2045         struct ll_sb_info *sbi = ll_i2sbi(inode);
2046         struct ptlrpc_request *req = NULL;
2047         int rc, flags = 0;
2048         ENTRY;
2049
2050         switch(cmd) {
2051         case FSFILT_IOC_GETFLAGS: {
2052                 struct ll_fid fid;
2053                 struct mds_body *body;
2054
2055                 ll_inode2fid(&fid, inode);
2056                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_FLFLAGS,0,&req);
2057                 if (rc) {
2058                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2059                         RETURN(-abs(rc));
2060                 }
2061
2062                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2063                                       sizeof(*body));
2064
2065                 /* We want to return EXT3_*_FL flags to the caller via this
2066                  * ioctl.  An older MDS may be sending S_* flags, fix it up. */
2067                 flags = ll_inode_to_ext_flags(body->flags,
2068                                               MDS_BFLAG_EXT_FLAGS);
2069                 ptlrpc_req_finished (req);
2070
2071                 RETURN(put_user(flags, (int *)arg));
2072         }
2073         case FSFILT_IOC_SETFLAGS: {
2074                 struct mdc_op_data op_data = { { 0 } };
2075                 struct ll_iattr_struct attr;
2076                 struct obd_info oinfo = { { { 0 } } };
2077                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
2078
2079                 if (get_user(flags, (int *)arg))
2080                         RETURN(-EFAULT);
2081
2082                 oinfo.oi_md = lsm;
2083                 OBDO_ALLOC(oinfo.oi_oa);
2084                 if (!oinfo.oi_oa)
2085                         RETURN(-ENOMEM);
2086
2087                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0, NULL);
2088
2089                 memset(&attr, 0, sizeof(attr));
2090                 attr.ia_attr_flags = flags;
2091                 ((struct iattr *)&attr)->ia_valid |= ATTR_ATTR_FLAG;
2092
2093                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
2094                                  (struct iattr *)&attr, NULL, 0, NULL, 0, &req);
2095                 ptlrpc_req_finished(req);
2096                 if (rc) {
2097                         OBDO_FREE(oinfo.oi_oa);
2098                         RETURN(rc);
2099                 }
2100
2101                 if (lsm == NULL) {
2102                         OBDO_FREE(oinfo.oi_oa);
2103                         GOTO(update_cache, rc);
2104                 }
2105
2106                 oinfo.oi_oa->o_id = lsm->lsm_object_id;
2107                 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
2108                 oinfo.oi_oa->o_flags = flags;
2109                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP |
2110                                        OBD_MD_FLFLAGS;
2111
2112                 obdo_from_inode(oinfo.oi_oa, inode,
2113                                 OBD_MD_FLFID | OBD_MD_FLGENER);
2114                 rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
2115                 OBDO_FREE(oinfo.oi_oa);
2116                 if (rc) {
2117                         if (rc != -EPERM && rc != -EACCES)
2118                                 CERROR("osc_setattr_async fails: rc = %d\n",rc);
2119                         RETURN(rc);
2120                 }
2121
2122                 EXIT;
2123 update_cache:
2124                 inode->i_flags = ll_ext_to_inode_flags(flags |
2125                                                        MDS_BFLAG_EXT_FLAGS);
2126                 return 0;
2127         }
2128         default:
2129                 RETURN(-ENOSYS);
2130         }
2131
2132         RETURN(0);
2133 }
2134
2135 /* umount -f client means force down, don't save state */
2136 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2137 void ll_umount_begin(struct vfsmount *vfsmnt, int flags)
2138 {
2139         struct super_block *sb = vfsmnt->mnt_sb;
2140 #else
2141 void ll_umount_begin(struct super_block *sb)
2142 {
2143 #endif
2144         struct lustre_sb_info *lsi = s2lsi(sb);
2145         struct ll_sb_info *sbi = ll_s2sbi(sb);
2146         struct obd_device *obd;
2147         struct obd_ioctl_data ioc_data = { 0 };
2148         ENTRY;
2149
2150 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2151         if (!(flags & MNT_FORCE)) {
2152                 EXIT;
2153                 return;
2154         }
2155 #endif
2156
2157         /* Tell the MGC we got umount -f */
2158         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
2159
2160         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2161                sb->s_count, atomic_read(&sb->s_active));
2162
2163         obd = class_exp2obd(sbi->ll_mdc_exp);
2164         if (obd == NULL) {
2165                 CERROR("Invalid MDC connection handle "LPX64"\n",
2166                        sbi->ll_mdc_exp->exp_handle.h_cookie);
2167                 EXIT;
2168                 return;
2169         }
2170         obd->obd_force = 1;
2171         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
2172                       &ioc_data, NULL);
2173
2174         obd = class_exp2obd(sbi->ll_osc_exp);
2175         if (obd == NULL) {
2176                 CERROR("Invalid LOV connection handle "LPX64"\n",
2177                        sbi->ll_osc_exp->exp_handle.h_cookie);
2178                 EXIT;
2179                 return;
2180         }
2181
2182         obd->obd_force = 1;
2183         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
2184                       &ioc_data, NULL);
2185
2186         /* Really, we'd like to wait until there are no requests outstanding,
2187          * and then continue.  For now, we just invalidate the requests,
2188          * schedule() and sleep one second if needed, and hope.
2189          */
2190         schedule();
2191 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2192         if (atomic_read(&vfsmnt->mnt_count) > 2) {
2193                 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
2194                                      cfs_time_seconds(1));
2195                 if (atomic_read(&vfsmnt->mnt_count) > 2)
2196                         LCONSOLE_WARN("Mount still busy with %d refs! You "
2197                                       "may try to umount it a bit later\n",
2198                                       atomic_read(&vfsmnt->mnt_count));
2199         }
2200 #endif
2201
2202         EXIT;
2203 }
2204
2205 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2206 {
2207         struct ll_sb_info *sbi = ll_s2sbi(sb);
2208         int err;
2209         __u32 read_only;
2210
2211         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2212                 read_only = *flags & MS_RDONLY;
2213                 err = obd_set_info_async(sbi->ll_mdc_exp, sizeof(KEY_READONLY),
2214                                          KEY_READONLY, sizeof(read_only),
2215                                          &read_only, NULL);
2216
2217                 /* MDS might have expected a different ro key value, b=17493 */
2218                 if (err == -EINVAL) {
2219                         CDEBUG(D_CONFIG, "Retrying remount with 1.6.6 ro key\n");
2220                         err = obd_set_info_async(sbi->ll_mdc_exp,
2221                                                  sizeof(KEY_READONLY_166COMPAT),
2222                                                  KEY_READONLY_166COMPAT,
2223                                                  sizeof(read_only),
2224                                                  &read_only, NULL);
2225                 }
2226
2227                 if (err) {
2228                         CERROR("Failed to change the read-only flag during "
2229                                "remount: %d\n", err);
2230                         return err;
2231                 }
2232
2233                 if (read_only)
2234                         sb->s_flags |= MS_RDONLY;
2235                 else
2236                         sb->s_flags &= ~MS_RDONLY;
2237         }
2238         return 0;
2239 }
2240
2241 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
2242                   struct ptlrpc_request *req, int offset,struct super_block *sb)
2243 {
2244         struct lustre_md md;
2245         struct ll_sb_info *sbi = NULL;
2246         int rc = 0;
2247         ENTRY;
2248
2249         LASSERT(*inode || sb);
2250         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2251
2252         rc = mdc_req2lustre_md(req, offset, exp, &md);
2253         if (rc)
2254                 RETURN(rc);
2255
2256         if (*inode) {
2257                 ll_update_inode(*inode, &md);
2258         } else {
2259                 LASSERT(sb);
2260                 /** hashing VFS inode by FIDs.
2261                  * IGIF will be used for for compatibility if needed.
2262                  */
2263                 *inode =ll_iget(sb, ll_fid_build_ino(&md.body->fid1), &md);
2264                 if (*inode == NULL || is_bad_inode(*inode)) {
2265                         mdc_free_lustre_md(exp, &md);
2266                         rc = -ENOMEM;
2267                         CERROR("new_inode -fatal: rc %d\n", rc);
2268                         GOTO(out, rc);
2269                 }
2270         }
2271
2272         rc = obd_checkmd(exp, ll_i2mdcexp(*inode),
2273                          ll_i2info(*inode)->lli_smd);
2274 out:
2275         RETURN(rc);
2276 }
2277
2278 char *llap_origins[] = {
2279         [LLAP_ORIGIN_UNKNOWN] = "--",
2280         [LLAP_ORIGIN_READPAGE] = "rp",
2281         [LLAP_ORIGIN_READAHEAD] = "ra",
2282         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
2283         [LLAP_ORIGIN_WRITEPAGE] = "wp"
2284 };
2285
2286 struct ll_async_page *llite_pglist_next_llap(struct list_head *head,
2287                                              struct list_head *list)
2288 {
2289         struct ll_async_page *llap;
2290         struct list_head *pos;
2291
2292         list_for_each(pos, list) {
2293                 if (pos == head)
2294                         return NULL;
2295                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
2296                 if (llap->llap_page == NULL)
2297                         continue;
2298                 return llap;
2299         }
2300         LBUG();
2301         return NULL;
2302 }
2303
2304 int ll_obd_statfs(struct inode *inode, void *arg)
2305 {
2306         struct ll_sb_info *sbi = NULL;
2307         struct obd_device *client_obd = NULL, *lov_obd = NULL;
2308         struct lov_obd *lov = NULL;
2309         struct obd_statfs stat_buf = {0};
2310         char *buf = NULL;
2311         struct obd_ioctl_data *data = NULL;
2312         __u32 type, index;
2313         int len = 0, rc;
2314
2315         if (!inode || !(sbi = ll_i2sbi(inode)))
2316                 GOTO(out_statfs, rc = -EINVAL);
2317
2318         rc = obd_ioctl_getdata(&buf, &len, arg);
2319         if (rc)
2320                 GOTO(out_statfs, rc);
2321
2322         data = (void*)buf;
2323         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2324             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2325                 GOTO(out_statfs, rc = -EINVAL);
2326
2327         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2328         memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2329
2330         if (type == LL_STATFS_MDC) {
2331                 if (index > 0)
2332                         GOTO(out_statfs, rc = -ENODEV);
2333                 client_obd = class_exp2obd(sbi->ll_mdc_exp);
2334         } else if (type == LL_STATFS_LOV) {
2335                 lov_obd = class_exp2obd(sbi->ll_osc_exp);
2336                 lov = &lov_obd->u.lov;
2337
2338                 if (index >= lov->desc.ld_tgt_count)
2339                         GOTO(out_statfs, rc = -ENODEV);
2340
2341                 if (!lov->lov_tgts[index])
2342                         /* Try again with the next index */
2343                         GOTO(out_statfs, rc = -EAGAIN);
2344
2345                 client_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2346                 if (!lov->lov_tgts[index]->ltd_active)
2347                         GOTO(out_uuid, rc = -ENODATA);
2348         }
2349
2350         if (!client_obd)
2351                 GOTO(out_statfs, rc = -EINVAL);
2352
2353         rc = obd_statfs(client_obd, &stat_buf,
2354                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 1);
2355         if (rc)
2356                 GOTO(out_uuid, rc);
2357
2358         if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
2359                 GOTO(out_statfs, rc = -EFAULT);
2360
2361 out_uuid:
2362         if (client_obd) {
2363                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(client_obd),
2364                                  data->ioc_plen2))
2365                         rc = -EFAULT;
2366         } else {
2367                 if (copy_to_user(data->ioc_pbuf2, "Unknown UUID",
2368                                  sizeof("Unknown UUID") + 1))
2369                         rc = -EFAULT;
2370         }
2371
2372 out_statfs:
2373         if (buf)
2374                 obd_ioctl_freedata(buf, len);
2375         return rc;
2376 }
2377
2378 int ll_process_config(struct lustre_cfg *lcfg)
2379 {
2380         char *ptr;
2381         void *sb;
2382         struct lprocfs_static_vars lvars;
2383         unsigned long x;
2384         int rc = 0;
2385
2386         lprocfs_llite_init_vars(&lvars);
2387
2388         /* The instance name contains the sb: lustre-client-aacfe000 */
2389         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2390         if (!ptr || !*(++ptr))
2391                 return -EINVAL;
2392         if (sscanf(ptr, "%lx", &x) != 1)
2393                 return -EINVAL;
2394         sb = (void *)x;
2395         /* This better be a real Lustre superblock! */
2396         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2397
2398         /* Note we have not called client_common_fill_super yet, so
2399            proc fns must be able to handle that! */
2400         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2401                                       lcfg, sb);
2402         return(rc);
2403 }
2404
2405 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs)
2406 {
2407         struct ll_sb_info *sbi;
2408
2409         LASSERT((seq != NULL) && (vfs != NULL));
2410         sbi = ll_s2sbi(vfs->mnt_sb);
2411
2412         if (sbi->ll_flags & LL_SBI_NOLCK)
2413                 seq_puts(seq, ",nolock");
2414
2415         if (sbi->ll_flags & LL_SBI_FLOCK)
2416                 seq_puts(seq, ",flock");
2417
2418         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2419                 seq_puts(seq, ",localflock");
2420
2421         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2422                 seq_puts(seq, ",user_xattr");
2423
2424         if (sbi->ll_flags & LL_SBI_ACL)
2425                 seq_puts(seq, ",acl");
2426
2427         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2428                 seq_puts(seq, ",lazystatfs");
2429
2430         RETURN(0);
2431 }