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