Whamcloud - gitweb
89af4ffdf42dfd9fe6cf2bd3092e8e8d7daa7035
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002-2005 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30
31 #include <linux/lustre_lite.h>
32 #include <linux/lustre_ha.h>
33 #include <linux/lustre_ver.h>
34 #include <linux/lustre_dlm.h>
35 #include <linux/lprocfs_status.h>
36 #include <linux/lustre_disk.h>
37 #include "llite_internal.h"
38
39 kmem_cache_t *ll_file_data_slab;
40
41 LIST_HEAD(ll_super_blocks);
42 spinlock_t ll_sb_lock = SPIN_LOCK_UNLOCKED;
43
44 extern struct address_space_operations ll_aops;
45 extern struct address_space_operations ll_dir_aops;
46
47 #ifndef log2
48 #define log2(n) ffz(~(n))
49 #endif
50
51 /* We need to have some extra twiddling here because some systems have
52  * no random state when they start up. */
53 static void
54 lustre_generate_random_uuid(class_uuid_t uuid)
55 {
56         struct timeval t;
57         int *i, j, k;
58
59         ENTRY;
60         LASSERT(sizeof(class_uuid_t) % sizeof(*i) == 0);
61
62         j = jiffies;
63         do_gettimeofday(&t);
64         k = t.tv_usec;
65
66         generate_random_uuid(uuid);
67
68         for (i = (int *)uuid; (char *)i < (char *)uuid + sizeof(class_uuid_t); i++) {
69                 *i ^= j ^ k;
70                 j = ((j << 8) & 0xffffff00) | ((j >> 24) & 0x000000ff);
71                 k = ((k >> 8) & 0x00ffffff) | ((k << 24) & 0xff000000);
72         }
73
74         EXIT;
75 }
76
77 struct ll_sb_info *ll_init_sbi(void)
78 {
79         struct ll_sb_info *sbi = NULL;
80         class_uuid_t uuid;
81         ENTRY;
82
83         OBD_ALLOC(sbi, sizeof(*sbi));
84         if (!sbi)
85                 RETURN(NULL);
86
87         spin_lock_init(&sbi->ll_lock);
88         spin_lock_init(&sbi->ll_lco.lco_lock);
89         INIT_LIST_HEAD(&sbi->ll_pglist);
90         sbi->ll_pglist_gen = 0;
91         if (num_physpages >> (20 - PAGE_SHIFT) < 512)
92                 sbi->ll_async_page_max = num_physpages / 2;
93         else
94                 sbi->ll_async_page_max = (num_physpages / 4) * 3;
95         sbi->ll_ra_info.ra_max_pages = min(num_physpages / 8,
96                                            SBI_DEFAULT_READAHEAD_MAX);
97
98         INIT_LIST_HEAD(&sbi->ll_conn_chain);
99         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
100
101         lustre_generate_random_uuid(uuid);
102         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
103         CDEBUG(D_HA, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
104
105         spin_lock(&ll_sb_lock);
106         list_add_tail(&sbi->ll_list, &ll_super_blocks);
107         spin_unlock(&ll_sb_lock);
108         RETURN(sbi);
109 }
110
111 void ll_free_sbi(struct super_block *sb)
112 {
113         struct ll_sb_info *sbi = ll_s2sbi(sb);
114         ENTRY;
115
116         if (sbi != NULL) {
117                 spin_lock(&ll_sb_lock);
118                 list_del(&sbi->ll_list);
119                 spin_unlock(&ll_sb_lock);
120                 OBD_FREE(sbi, sizeof(*sbi));
121         }
122         EXIT;
123 }
124
125 int client_common_fill_super(struct super_block *sb, char *mdc, char *osc)
126 {
127         struct inode *root = 0;
128         struct ll_sb_info *sbi = ll_s2sbi(sb);
129         struct obd_device *obd;
130         struct ll_fid rootfid;
131         struct obd_statfs osfs;
132         struct ptlrpc_request *request = NULL;
133         struct lustre_handle osc_conn = {0, };
134         struct lustre_handle mdc_conn = {0, };
135         struct lustre_md md;
136         struct obd_connect_data *data = NULL;
137         int err;
138         ENTRY;
139
140         obd = class_name2obd(mdc);
141         if (!obd) {
142                 CERROR("MDC %s: not setup or attached\n", mdc);
143                 RETURN(-EINVAL);
144         }
145
146         OBD_ALLOC(data, sizeof(*data));
147         if (data == NULL)
148                 RETURN(-ENOMEM);
149
150         if (proc_lustre_fs_root) {
151                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
152                                                   osc, mdc);
153                 if (err < 0)
154                         CERROR("could not register mount in /proc/lustre");
155         }
156
157         if (sb->s_flags & MS_RDONLY)
158                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
159
160         if (sbi->ll_flags & LL_SBI_FLOCK) {
161                 sbi->ll_fop = &ll_file_operations_flock;
162         } else {
163                 sbi->ll_fop = &ll_file_operations;
164         }
165
166         data->ocd_connect_flags |= OBD_CONNECT_VERSION;
167         data->ocd_version = LUSTRE_VERSION_CODE;
168         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, data);
169         if (err == -EBUSY) {
170                 CERROR("An MDT (mdc %s) is performing recovery, of which this"
171                        " client is not a part.  Please wait for recovery to "
172                        "complete, abort, or time out.\n", mdc);
173                 GOTO(out, err);
174         } else if (err) {
175                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
176                 GOTO(out, err);
177         }
178         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
179
180         err = obd_statfs(obd, &osfs, jiffies - HZ);
181         if (err)
182                 GOTO(out_mdc, err);
183
184         LASSERT(osfs.os_bsize);
185         sb->s_blocksize = osfs.os_bsize;
186         sb->s_blocksize_bits = log2(osfs.os_bsize);
187         sb->s_magic = LL_SUPER_MAGIC;
188         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
189         sbi->ll_namelen = osfs.os_namelen;
190
191 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
192         /* We set sb->s_dev equal on all lustre clients in order to support
193          * NFS export clustering.  NFSD requires that the FSID be the same
194          * on all clients. */
195         /* s_dev is also used in lt_compare() to compare two fs, but that is
196          * only a node-local comparison. */
197         sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid,
198                          strlen(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid));
199 #endif
200
201         obd = class_name2obd(osc);
202         if (!obd) {
203                 CERROR("OSC %s: not setup or attached\n", osc);
204                 GOTO(out_mdc, err);
205         }
206
207         data->ocd_connect_flags =
208                 OBD_CONNECT_GRANT|OBD_CONNECT_VERSION|OBD_CONNECT_REQPORTAL;
209
210         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
211                "ocd_grant: %d\n", data->ocd_connect_flags,
212                data->ocd_version, data->ocd_grant);
213
214         obd->obd_upcall.onu_owner = &sbi->ll_lco;
215         obd->obd_upcall.onu_upcall = ll_ocd_update;
216
217         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, data);
218         if (err == -EBUSY) {
219                 CERROR("An OST (osc %s) is performing recovery, of which this"
220                        " client is not a part.  Please wait for recovery to "
221                        "complete, abort, or time out.\n", osc);
222                 GOTO(out, err);
223         } else if (err) {
224                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
225                 GOTO(out_mdc, err);
226         }
227         sbi->ll_osc_exp = class_conn2export(&osc_conn);
228         spin_lock(&sbi->ll_lco.lco_lock);
229         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
230         spin_unlock(&sbi->ll_lco.lco_lock);
231
232         mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
233
234         if (!ll_async_page_slab) {
235                 ll_async_page_slab_size =
236                         size_round(sizeof(struct ll_async_page)) +
237                         obd_prep_async_page(sbi->ll_osc_exp, NULL, NULL, NULL,
238                                             0, NULL, NULL, NULL);
239                 ll_async_page_slab = kmem_cache_create("ll_async_page",
240                                                        ll_async_page_slab_size,
241                                                        0, 0, NULL, NULL);
242                 if (!ll_async_page_slab)
243                         GOTO(out_osc, -ENOMEM);
244         }
245
246         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
247         if (err) {
248                 CERROR("cannot mds_connect: rc = %d\n", err);
249                 GOTO(out_osc, err);
250         }
251         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
252         sbi->ll_rootino = rootfid.id;
253
254         sb->s_op = &lustre_super_operations;
255
256         /* make root inode
257          * XXX: move this to after cbd setup? */
258         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
259                           OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
260         if (err) {
261                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
262                 GOTO(out_osc, err);
263         }
264
265         err = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
266         if (err) {
267                 CERROR("failed to understand root inode md: rc = %d\n",err);
268                 ptlrpc_req_finished (request);
269                 GOTO(out_osc, err);
270         }
271
272         LASSERT(sbi->ll_rootino != 0);
273         root = ll_iget(sb, sbi->ll_rootino, &md);
274
275         ptlrpc_req_finished(request);
276
277         if (root == NULL || is_bad_inode(root)) {
278                 if (md.lsm != NULL)
279                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
280                 CERROR("lustre_lite: bad iget4 for root\n");
281                 GOTO(out_root, err = -EBADF);
282         }
283
284         err = ll_close_thread_start(&sbi->ll_lcq);
285         if (err) {
286                 CERROR("cannot start close thread: rc %d\n", err);
287                 GOTO(out_root, err);
288         }
289
290         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
291            backing dev info assigned to inode mapping is used for
292            determining maximal readahead. */
293 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
294     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
295         /* bug 2805 - set VM readahead to zero */
296         vm_max_readahead = vm_min_readahead = 0;
297 #endif
298
299         sb->s_root = d_alloc_root(root);
300         if (data != NULL)
301                 OBD_FREE(data, sizeof(*data));
302         RETURN(err);
303
304 out_root:
305         if (root)
306                 iput(root);
307 out_osc:
308         obd_disconnect(sbi->ll_osc_exp);
309 out_mdc:
310         obd_disconnect(sbi->ll_mdc_exp);
311 out:
312         if (data != NULL)
313                 OBD_FREE(data, sizeof(*data));
314         lprocfs_unregister_mountpoint(sbi);
315         RETURN(err);
316 }
317
318 void ll_dump_inode(struct inode *inode)
319 {
320         struct list_head *tmp;
321         int dentry_count = 0;
322
323         LASSERT(inode != NULL);
324
325         list_for_each(tmp, &inode->i_dentry)
326                 dentry_count++;
327
328         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
329                inode, ll_i2mdcexp(inode)->exp_obd->obd_name, inode->i_ino,
330                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
331 }
332
333 void lustre_dump_dentry(struct dentry *dentry, int recur)
334 {
335         struct list_head *tmp;
336         int subdirs = 0;
337
338         LASSERT(dentry != NULL);
339
340         list_for_each(tmp, &dentry->d_subdirs)
341                 subdirs++;
342
343         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
344                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
345                dentry->d_name.len, dentry->d_name.name,
346                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
347                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
348                dentry->d_flags, dentry->d_fsdata, subdirs);
349         if (dentry->d_inode != NULL)
350                 ll_dump_inode(dentry->d_inode);
351
352         if (recur == 0)
353                 return;
354
355         list_for_each(tmp, &dentry->d_subdirs) {
356                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
357                 lustre_dump_dentry(d, recur - 1);
358         }
359 }
360
361 void client_common_put_super(struct super_block *sb)
362 {
363         struct ll_sb_info *sbi = ll_s2sbi(sb);
364         struct hlist_node *tmp, *next;
365         ENTRY;
366
367         ll_close_thread_shutdown(sbi->ll_lcq);
368
369         list_del(&sbi->ll_conn_chain);
370         obd_disconnect(sbi->ll_osc_exp);
371
372         lprocfs_unregister_mountpoint(sbi);
373         if (sbi->ll_proc_root) {
374                 lprocfs_remove(sbi->ll_proc_root);
375                 sbi->ll_proc_root = NULL;
376         }
377
378         obd_disconnect(sbi->ll_mdc_exp);
379
380         // We do this to get rid of orphaned dentries. That is not really trw.
381         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
382                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
383                 CWARN("found orphan dentry %.*s (%p->%p) at unmount, dumping "
384                       "before and after shrink_dcache_parent\n",
385                       dentry->d_name.len, dentry->d_name.name, dentry, next);
386                 lustre_dump_dentry(dentry, 1);
387                 shrink_dcache_parent(dentry);
388                 lustre_dump_dentry(dentry, 1);
389         }
390         EXIT;
391 }
392
393 char *ll_read_opt(const char *opt, char *data)
394 {
395         char *value;
396         char *retval;
397         ENTRY;
398
399         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
400         if (strncmp(opt, data, strlen(opt)))
401                 RETURN(NULL);
402         if ((value = strchr(data, '=')) == NULL)
403                 RETURN(NULL);
404
405         value++;
406         OBD_ALLOC(retval, strlen(value) + 1);
407         if (!retval) {
408                 CERROR("out of memory!\n");
409                 RETURN(NULL);
410         }
411
412         memcpy(retval, value, strlen(value)+1);
413         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
414         RETURN(retval);
415 }
416
417 int ll_set_opt(const char *opt, char *data, int fl)
418 {
419         ENTRY;
420
421         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
422         if (strncmp(opt, data, strlen(opt)))
423                 RETURN(0);
424         else
425                 RETURN(fl);
426 }
427
428 void ll_options(char *options, int *flags)
429 {
430         int tmp;
431         char *this_char;
432 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
433         char *opt_ptr = options;
434 #endif
435         ENTRY;
436
437         if (!options) {
438                 EXIT;
439                 return;
440         }
441
442         CERROR("Parsing opts %s\n", options);
443 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
444         for (this_char = strtok (options, ",");
445              this_char != NULL;
446              this_char = strtok (NULL, ","))
447 #else
448         while ((this_char = strsep (&opt_ptr, ",")) != NULL)
449 #endif
450         {
451                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
452                 tmp = ll_set_opt("nolock", this_char, LL_SBI_NOLCK);
453                 if (tmp) {
454                         *flags |= tmp;
455                         continue;
456                 }
457                 tmp = ll_set_opt("flock", this_char, LL_SBI_FLOCK);
458                 if (tmp) {
459                         *flags |= tmp;
460                         continue;
461                 }
462                 tmp = ll_set_opt("noflock", this_char, LL_SBI_FLOCK);
463                 if (tmp) {
464                         *flags &= ~tmp;
465                         continue;
466                 }
467                 tmp = ll_set_opt("user_xattr", this_char, LL_SBI_USER_XATTR);
468                 if (tmp) {
469                         *flags |= tmp;
470                         continue;
471                 }
472                 tmp = ll_set_opt("nouser_xattr", this_char, LL_SBI_USER_XATTR);
473                 if (tmp) {
474                         *flags &= ~tmp;
475                         continue;
476                 }
477         }
478         CERROR("flags %#x\n", *flags);
479
480         EXIT;
481 }
482                 
483 void ll_lli_init(struct ll_inode_info *lli)
484 {
485         sema_init(&lli->lli_open_sem, 1);
486         sema_init(&lli->lli_size_sem, 1);
487         lli->lli_flags = 0;
488         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
489         spin_lock_init(&lli->lli_lock);
490         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
491         lli->lli_inode_magic = LLI_INODE_MAGIC;
492 }
493
494 int ll_fill_super(struct super_block *sb)
495 {
496         struct lustre_profile *lprof;
497         struct lustre_sb_info *lsi = s2lsi(sb);
498         struct ll_sb_info *sbi;
499         char  *osc = NULL;
500         char  *mdc = NULL;
501         char  *profilenm = get_profile_name(sb);
502         struct config_llog_instance cfg;
503         char   ll_instance[sizeof(sb) * 2 + 1];
504         int    err;
505         ENTRY;
506                                                                                  
507         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
508
509         /* client additional sb info */
510         lsi->lsi_llsbi = sbi = ll_init_sbi();
511         if (!sbi) 
512                 RETURN(-ENOMEM);
513
514         ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
515
516         /* Generate a string unique to this super, in case some joker tries
517            to mount the same fs at two mount points. 
518            Use the address of the super itself.*/
519         sprintf(ll_instance, "%p", sb);
520         cfg.cfg_instance = ll_instance;
521         cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
522         
523         /* set up client obds */
524         err = lustre_get_process_log(sb, profilenm, &cfg);
525         if (err < 0) {
526                 CERROR("Unable to process log: %d\n", err);
527                 GOTO(out_free, err);
528         }
529
530         lprof = class_get_profile(profilenm);
531         if (lprof == NULL) {
532                 CERROR("No profile found: %s\n", profilenm);
533                 GOTO(out_free, err = -EINVAL);
534         }
535         CERROR("Found profile %s: mdc=%s osc=%s\n", profilenm, 
536                lprof->lp_mdc, lprof->lp_osc);
537
538         OBD_ALLOC(osc, strlen(lprof->lp_osc) +
539                   strlen(ll_instance) + 2);
540         if (!osc) 
541                 GOTO(out_free, err = -ENOMEM);
542         sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
543
544         OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
545                   strlen(ll_instance) + 2);
546         if (!mdc) 
547                 GOTO(out_free, err = -ENOMEM);
548         sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
549   
550         /* connections, registrations, sb setup */
551         err = client_common_fill_super(sb, mdc, osc);
552   
553 out_free:
554         if (mdc)
555                 OBD_FREE(mdc, strlen(mdc) + 1);
556         if (osc)
557                 OBD_FREE(osc, strlen(osc) + 1);
558         if (err) {
559                 struct obd_device *obd;
560                 int next = 0;
561                 /* like client_put_super below */
562                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) 
563                        != NULL) {
564                         class_manual_cleanup(obd);
565                 }                       
566                 class_del_profile(profilenm);
567                 ll_free_sbi(sb);
568                 lsi->lsi_llsbi = NULL;
569         }
570         RETURN(err);
571 } /* ll_fill_super */
572
573
574 void ll_put_super(struct super_block *sb)
575 {
576         struct obd_device *obd;
577         struct lustre_sb_info *lsi = s2lsi(sb);
578         struct ll_sb_info *sbi = ll_s2sbi(sb);
579         char *profilenm = get_profile_name(sb);
580         int next = 0;
581         ENTRY;
582
583         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
584         obd = class_exp2obd(sbi->ll_mdc_exp);
585         if (obd) {
586                 int next = 0;
587                 int force = obd->obd_no_recov;
588                 /* We need to set force before the lov_disconnect in 
589                 lustre_common_put_super, since l_d cleans up osc's as well. */
590                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) 
591                        != NULL) {
592                         obd->obd_force = force;
593                 }                       
594         }
595
596         client_common_put_super(sb);
597                 
598         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
599                 class_manual_cleanup(obd);
600         }                       
601         
602         if (profilenm) 
603                 class_del_profile(profilenm);
604         
605         ll_free_sbi(sb);
606         lsi->lsi_llsbi = NULL;
607
608         lustre_common_put_super(sb);
609
610         EXIT;
611 } /* client_put_super */
612
613 #ifdef HAVE_REGISTER_CACHE
614 #include <linux/cache_def.h>
615 #ifdef HAVE_CACHE_RETURN_INT
616 static int
617 #else
618 static void
619 #endif
620 ll_shrink_cache(int priority, unsigned int gfp_mask)
621 {
622         struct ll_sb_info *sbi;
623         int count = 0;
624
625         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
626                 count += llap_shrink_cache(sbi, priority);
627
628 #ifdef HAVE_CACHE_RETURN_INT
629         return count;
630 #endif
631 }
632
633 struct cache_definition ll_cache_definition = {
634         .name = "llap_cache",
635         .shrink = ll_shrink_cache
636 };
637 #endif /* HAVE_REGISTER_CACHE */
638
639 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
640 {
641         struct inode *inode = NULL;
642         l_lock(&lock->l_resource->lr_namespace->ns_lock);
643         if (lock->l_ast_data) {
644                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
645                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
646                         inode = igrab(lock->l_ast_data);
647                 } else {
648                         inode = lock->l_ast_data;
649                         __LDLM_DEBUG(inode->i_state & I_FREEING ?
650                                      D_INFO : D_WARNING, lock,
651                                      "l_ast_data %p is bogus: magic %08x",
652                                      lock->l_ast_data, lli->lli_inode_magic);
653                         inode = NULL;
654                 }
655         }
656         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
657         return inode;
658 }
659
660 static int null_if_equal(struct ldlm_lock *lock, void *data)
661 {
662         if (data == lock->l_ast_data) {
663                 lock->l_ast_data = NULL;
664
665                 if (lock->l_req_mode != lock->l_granted_mode)
666                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
667         }
668
669         return LDLM_ITER_CONTINUE;
670 }
671
672 void ll_clear_inode(struct inode *inode)
673 {
674         struct ll_fid fid;
675         struct ll_inode_info *lli = ll_i2info(inode);
676         struct ll_sb_info *sbi = ll_i2sbi(inode);
677         ENTRY;
678
679         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
680                inode->i_generation, inode);
681
682         ll_inode2fid(&fid, inode);
683         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
684         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
685
686         if (lli->lli_smd) {
687                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
688                                   null_if_equal, inode);
689
690                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
691                 lli->lli_smd = NULL;
692         }
693
694         if (lli->lli_symlink_name) {
695                 OBD_FREE(lli->lli_symlink_name,
696                          strlen(lli->lli_symlink_name) + 1);
697                 lli->lli_symlink_name = NULL;
698         }
699         lli->lli_inode_magic = LLI_INODE_DEAD;
700
701         EXIT;
702 }
703
704 /* If this inode has objects allocated to it (lsm != NULL), then the OST
705  * object(s) determine the file size and mtime.  Otherwise, the MDS will
706  * keep these values until such a time that objects are allocated for it.
707  * We do the MDS operations first, as it is checking permissions for us.
708  * We don't to the MDS RPC if there is nothing that we want to store there,
709  * otherwise there is no harm in updating mtime/atime on the MDS if we are
710  * going to do an RPC anyways.
711  *
712  * If we are doing a truncate, we will send the mtime and ctime updates
713  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
714  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
715  * at the same time.
716  */
717 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
718 {
719         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
720         struct ll_sb_info *sbi = ll_i2sbi(inode);
721         struct ptlrpc_request *request = NULL;
722         struct mdc_op_data op_data;
723         int ia_valid = attr->ia_valid;
724         int rc = 0;
725         ENTRY;
726
727         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
728                attr->ia_valid);
729         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
730
731         if (ia_valid & ATTR_SIZE) {
732                 if (attr->ia_size > ll_file_maxbytes(inode)) {
733                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
734                                attr->ia_size, ll_file_maxbytes(inode));
735                         RETURN(-EFBIG);
736                 }
737
738                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
739         }
740
741         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
742         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
743                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
744                         RETURN(-EPERM);
745         }
746
747         /* We mark all of the fields "set" so MDS/OST does not re-set them */
748         if (attr->ia_valid & ATTR_CTIME) {
749                 attr->ia_ctime = CURRENT_TIME;
750                 attr->ia_valid |= ATTR_CTIME_SET;
751         }
752         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
753                 attr->ia_atime = CURRENT_TIME;
754                 attr->ia_valid |= ATTR_ATIME_SET;
755         }
756         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
757                 attr->ia_mtime = CURRENT_TIME;
758                 attr->ia_valid |= ATTR_MTIME_SET;
759         }
760
761         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
762                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
763                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
764                        CURRENT_SECONDS);
765
766
767         /* NB: ATTR_SIZE will only be set after this point if the size
768          * resides on the MDS, ie, this file has no objects. */
769         if (lsm)
770                 attr->ia_valid &= ~ATTR_SIZE;
771
772         /* If only OST attributes being set on objects, don't do MDS RPC.
773          * In that case, we need to check permissions and update the local
774          * inode ourselves so we can call obdo_from_inode() always. */
775         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
776                 struct lustre_md md;
777                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
778
779                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
780                                  attr, NULL, 0, NULL, 0, &request);
781
782                 if (rc) {
783                         ptlrpc_req_finished(request);
784                         if (rc != -EPERM && rc != -EACCES)
785                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
786                         RETURN(rc);
787                 }
788
789                 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
790                 if (rc) {
791                         ptlrpc_req_finished(request);
792                         RETURN(rc);
793                 }
794
795                 /* We call inode_setattr to adjust timestamps.
796                  * If there is at least some data in file, we cleared ATTR_SIZE
797                  * above to avoid invoking vmtruncate, otherwise it is important
798                  * to call vmtruncate in inode_setattr to update inode->i_size
799                  * (bug 6196) */
800                 inode_setattr(inode, attr);
801
802                 ll_update_inode(inode, md.body, md.lsm);
803                 ptlrpc_req_finished(request);
804
805                 if (!lsm || !S_ISREG(inode->i_mode)) {
806                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
807                         RETURN(0);
808                 }
809         } else {
810                 /* The OST doesn't check permissions, but the alternative is
811                  * a gratuitous RPC to the MDS.  We already rely on the client
812                  * to do read/write/truncate permission checks, so is mtime OK?
813                  */
814                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
815                         /* from sys_utime() */
816                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
817                                 if (current->fsuid != inode->i_uid &&
818                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
819                                         RETURN(rc);
820                         } else {
821                                 /* from inode_change_ok() */
822                                 if (current->fsuid != inode->i_uid &&
823                                     !capable(CAP_FOWNER))
824                                         RETURN(-EPERM);
825                         }
826                 }
827
828                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
829                 inode_setattr(inode, attr);
830         }
831
832         /* We really need to get our PW lock before we change inode->i_size.
833          * If we don't we can race with other i_size updaters on our node, like
834          * ll_file_read.  We can also race with i_size propogation to other
835          * nodes through dirtying and writeback of final cached pages.  This
836          * last one is especially bad for racing o_append users on other
837          * nodes. */
838         if (ia_valid & ATTR_SIZE) {
839                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
840                                                            OBD_OBJECT_EOF } };
841                 struct lustre_handle lockh = { 0 };
842                 struct ll_inode_info *lli = ll_i2info(inode);
843                 int err, ast_flags = 0;
844                 /* XXX when we fix the AST intents to pass the discard-range
845                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
846                  * XXX here. */
847                 if (attr->ia_size == 0)
848                         ast_flags = LDLM_AST_DISCARD_DATA;
849
850                 up(&inode->i_sem);
851                 UP_WRITE_I_ALLOC_SEM(inode);
852                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
853                                     ast_flags);
854 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
855                 DOWN_WRITE_I_ALLOC_SEM(inode);
856                 down(&inode->i_sem);
857 #else
858                 down(&inode->i_sem);
859                 DOWN_WRITE_I_ALLOC_SEM(inode);
860 #endif
861                 if (rc != 0)
862                         RETURN(rc);
863
864                 /* Only ll_inode_size_lock is taken at this level.
865                  * lov_stripe_lock() is grabbed by ll_truncate() only over
866                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
867                  * ll_truncate dropped ll_inode_size_lock() */
868                 ll_inode_size_lock(inode, 0);
869                 rc = vmtruncate(inode, attr->ia_size);
870                 if (rc != 0) {
871                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
872                         ll_inode_size_unlock(inode, 0);
873                 }
874
875                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
876                 if (err) {
877                         CERROR("ll_extent_unlock failed: %d\n", err);
878                         if (!rc)
879                                 rc = err;
880                 }
881         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
882                 struct obdo oa;
883
884                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
885                        inode->i_ino, LTIME_S(attr->ia_mtime));
886                 oa.o_id = lsm->lsm_object_id;
887                 oa.o_valid = OBD_MD_FLID;
888                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
889                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
890                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
891                 if (rc)
892                         CERROR("obd_setattr fails: rc=%d\n", rc);
893         }
894         RETURN(rc);
895 }
896
897 int ll_setattr(struct dentry *de, struct iattr *attr)
898 {
899         LBUG(); /* code is unused, but leave this in case of VFS changes */
900         RETURN(-ENOSYS);
901 }
902
903 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
904                        unsigned long max_age)
905 {
906         struct ll_sb_info *sbi = ll_s2sbi(sb);
907         struct obd_statfs obd_osfs;
908         int rc;
909         ENTRY;
910
911         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
912         if (rc) {
913                 CERROR("mdc_statfs fails: rc = %d\n", rc);
914                 RETURN(rc);
915         }
916
917         osfs->os_type = sb->s_magic;
918
919         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
920                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
921
922         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
923         if (rc) {
924                 CERROR("obd_statfs fails: rc = %d\n", rc);
925                 RETURN(rc);
926         }
927
928         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
929                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
930                obd_osfs.os_files);
931
932         osfs->os_blocks = obd_osfs.os_blocks;
933         osfs->os_bfree = obd_osfs.os_bfree;
934         osfs->os_bavail = obd_osfs.os_bavail;
935
936         /* If we don't have as many objects free on the OST as inodes
937          * on the MDS, we reduce the total number of inodes to
938          * compensate, so that the "inodes in use" number is correct.
939          */
940         if (obd_osfs.os_ffree < osfs->os_ffree) {
941                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
942                         obd_osfs.os_ffree;
943                 osfs->os_ffree = obd_osfs.os_ffree;
944         }
945
946         RETURN(rc);
947 }
948
949 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
950 {
951         struct obd_statfs osfs;
952         int rc;
953
954         CDEBUG(D_VFSTRACE, "VFS Op:\n");
955         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
956
957         /* For now we will always get up-to-date statfs values, but in the
958          * future we may allow some amount of caching on the client (e.g.
959          * from QOS or lprocfs updates). */
960         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
961         if (rc)
962                 return rc;
963
964         statfs_unpack(sfs, &osfs);
965
966         if (sizeof(sfs->f_blocks) == 4) {
967                 while (osfs.os_blocks > ~0UL) {
968                         sfs->f_bsize <<= 1;
969
970                         osfs.os_blocks >>= 1;
971                         osfs.os_bfree >>= 1;
972                         osfs.os_bavail >>= 1;
973                 }
974         }
975
976         sfs->f_blocks = osfs.os_blocks;
977         sfs->f_bfree = osfs.os_bfree;
978         sfs->f_bavail = osfs.os_bavail;
979
980         return 0;
981 }
982
983 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
984 {
985         struct ll_inode_info *lli;
986         struct lov_stripe_md *lsm;
987
988         lli = ll_i2info(inode);
989         LASSERT(lli->lli_size_sem_owner != current);
990         down(&lli->lli_size_sem);
991         LASSERT(lli->lli_size_sem_owner == NULL);
992         lli->lli_size_sem_owner = current;
993         lsm = lli->lli_smd;
994         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
995                  lsm, lock_lsm);
996         if (lock_lsm)
997                 lov_stripe_lock(lsm);
998 }
999
1000 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1001 {
1002         struct ll_inode_info *lli;
1003         struct lov_stripe_md *lsm;
1004
1005         lli = ll_i2info(inode);
1006         lsm = lli->lli_smd;
1007         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1008                  lsm, unlock_lsm);
1009         if (unlock_lsm)
1010                 lov_stripe_unlock(lsm);
1011         LASSERT(lli->lli_size_sem_owner == current);
1012         lli->lli_size_sem_owner = NULL;
1013         up(&lli->lli_size_sem);
1014 }
1015
1016 void ll_update_inode(struct inode *inode, struct mds_body *body,
1017                      struct lov_stripe_md *lsm)
1018 {
1019         struct ll_inode_info *lli = ll_i2info(inode);
1020
1021         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1022         if (lsm != NULL) {
1023                 if (lli->lli_smd == NULL) {
1024                         if (lsm->lsm_magic != LOV_MAGIC) {
1025                                 dump_lsm(D_ERROR, lsm);
1026                                 LBUG();
1027                         }
1028                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1029                                lsm, inode->i_ino, inode->i_generation, inode);
1030                         /* ll_inode_size_lock() requires it is only called
1031                          * with lli_smd != NULL or lock_lsm == 0 or we can
1032                          * race between lock/unlock.  bug 9547 */
1033                         lli->lli_smd = lsm;
1034                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1035                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1036                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1037                 } else {
1038                         if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1039                                 CERROR("lsm mismatch for inode %ld\n",
1040                                        inode->i_ino);
1041                                 CERROR("lli_smd:\n");
1042                                 dump_lsm(D_ERROR, lli->lli_smd);
1043                                 CERROR("lsm:\n");
1044                                 dump_lsm(D_ERROR, lsm);
1045                                 LBUG();
1046                         }
1047                 }
1048                 /* bug 2844 - limit i_blksize for broken user-space apps */
1049                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1050                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1051                 if (lli->lli_smd != lsm)
1052                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1053         } else {
1054                 inode->i_blksize = max(inode->i_blksize,
1055                                        inode->i_sb->s_blocksize);
1056         }
1057
1058         if (body->valid & OBD_MD_FLID)
1059                 inode->i_ino = body->ino;
1060         if (body->valid & OBD_MD_FLATIME)
1061                 LTIME_S(inode->i_atime) = body->atime;
1062         if (body->valid & OBD_MD_FLMTIME &&
1063             body->mtime > LTIME_S(inode->i_mtime)) {
1064                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to "LPU64"\n",
1065                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1066                 LTIME_S(inode->i_mtime) = body->mtime;
1067         }
1068         if (body->valid & OBD_MD_FLCTIME &&
1069             body->ctime > LTIME_S(inode->i_ctime))
1070                 LTIME_S(inode->i_ctime) = body->ctime;
1071         if (body->valid & OBD_MD_FLMODE)
1072                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1073         if (body->valid & OBD_MD_FLTYPE)
1074                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1075         if (body->valid & OBD_MD_FLUID)
1076                 inode->i_uid = body->uid;
1077         if (body->valid & OBD_MD_FLGID)
1078                 inode->i_gid = body->gid;
1079         if (body->valid & OBD_MD_FLFLAGS)
1080                 inode->i_flags = body->flags;
1081         if (body->valid & OBD_MD_FLNLINK)
1082                 inode->i_nlink = body->nlink;
1083         if (body->valid & OBD_MD_FLGENER)
1084                 inode->i_generation = body->generation;
1085         if (body->valid & OBD_MD_FLRDEV)
1086 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1087                 inode->i_rdev = body->rdev;
1088 #else
1089                 inode->i_rdev = old_decode_dev(body->rdev);
1090 #endif
1091         if (body->valid & OBD_MD_FLSIZE)
1092                 inode->i_size = body->size;
1093         if (body->valid & OBD_MD_FLBLOCKS)
1094                 inode->i_blocks = body->blocks;
1095
1096         if (body->valid & OBD_MD_FLSIZE)
1097                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1098 }
1099
1100 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1101 static struct backing_dev_info ll_backing_dev_info = {
1102         .ra_pages       = 0,    /* No readahead */
1103 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
1104         .capabilities   = 0,    /* Does contribute to dirty memory */
1105 #else
1106         .memory_backed  = 0,    /* Does contribute to dirty memory */
1107 #endif
1108 };
1109 #endif
1110
1111 void ll_read_inode2(struct inode *inode, void *opaque)
1112 {
1113         struct lustre_md *md = opaque;
1114         struct ll_inode_info *lli = ll_i2info(inode);
1115         ENTRY;
1116
1117         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1118                inode->i_generation, inode);
1119
1120         ll_lli_init(lli);
1121
1122         LASSERT(!lli->lli_smd);
1123
1124         /* Core attributes from the MDS first.  This is a new inode, and
1125          * the VFS doesn't zero times in the core inode so we have to do
1126          * it ourselves.  They will be overwritten by either MDS or OST
1127          * attributes - we just need to make sure they aren't newer. */
1128         LTIME_S(inode->i_mtime) = 0;
1129         LTIME_S(inode->i_atime) = 0;
1130         LTIME_S(inode->i_ctime) = 0;
1131         inode->i_rdev = 0;
1132         ll_update_inode(inode, md->body, md->lsm);
1133
1134         /* OIDEBUG(inode); */
1135
1136         if (S_ISREG(inode->i_mode)) {
1137                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1138                 inode->i_op = &ll_file_inode_operations;
1139                 inode->i_fop = sbi->ll_fop;
1140                 inode->i_mapping->a_ops = &ll_aops;
1141                 EXIT;
1142         } else if (S_ISDIR(inode->i_mode)) {
1143                 inode->i_op = &ll_dir_inode_operations;
1144                 inode->i_fop = &ll_dir_operations;
1145                 inode->i_mapping->a_ops = &ll_dir_aops;
1146                 EXIT;
1147         } else if (S_ISLNK(inode->i_mode)) {
1148                 inode->i_op = &ll_fast_symlink_inode_operations;
1149                 EXIT;
1150         } else {
1151                 inode->i_op = &ll_special_inode_operations;
1152
1153 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1154                 init_special_inode(inode, inode->i_mode,
1155                                    kdev_t_to_nr(inode->i_rdev));
1156
1157                 /* initializing backing dev info. */
1158                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1159 #else
1160                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1161 #endif
1162                 lli->ll_save_ifop = inode->i_fop;
1163
1164                 if (S_ISCHR(inode->i_mode))
1165                         inode->i_fop = &ll_special_chr_inode_fops;
1166                 else if (S_ISBLK(inode->i_mode))
1167                         inode->i_fop = &ll_special_blk_inode_fops;
1168                 else if (S_ISFIFO(inode->i_mode))
1169                         inode->i_fop = &ll_special_fifo_inode_fops;
1170                 else if (S_ISSOCK(inode->i_mode))
1171                         inode->i_fop = &ll_special_sock_inode_fops;
1172                 EXIT;
1173         }
1174 }
1175
1176 int ll_iocontrol(struct inode *inode, struct file *file,
1177                  unsigned int cmd, unsigned long arg)
1178 {
1179         struct ll_sb_info *sbi = ll_i2sbi(inode);
1180         struct ptlrpc_request *req = NULL;
1181         int rc, flags = 0;
1182         ENTRY;
1183
1184         switch(cmd) {
1185         case EXT3_IOC_GETFLAGS: {
1186                 struct ll_fid fid;
1187                 struct mds_body *body;
1188
1189                 ll_inode2fid(&fid, inode);
1190                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_FLFLAGS,0,&req);
1191                 if (rc) {
1192                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1193                         RETURN(-abs(rc));
1194                 }
1195
1196                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1197
1198                 if (body->flags & S_APPEND)
1199                         flags |= EXT3_APPEND_FL;
1200                 if (body->flags & S_IMMUTABLE)
1201                         flags |= EXT3_IMMUTABLE_FL;
1202                 if (body->flags & S_NOATIME)
1203                         flags |= EXT3_NOATIME_FL;
1204
1205                 ptlrpc_req_finished (req);
1206
1207                 RETURN(put_user(flags, (int *)arg));
1208         }
1209         case EXT3_IOC_SETFLAGS: {
1210                 struct mdc_op_data op_data;
1211                 struct iattr attr;
1212                 struct obdo *oa;
1213                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1214
1215                 if (get_user(flags, (int *)arg))
1216                         RETURN(-EFAULT);
1217
1218                 oa = obdo_alloc();
1219                 if (!oa)
1220                         RETURN(-ENOMEM);
1221
1222                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1223
1224                 memset(&attr, 0x0, sizeof(attr));
1225                 attr.ia_attr_flags = flags;
1226                 attr.ia_valid |= ATTR_ATTR_FLAG;
1227
1228                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1229                                  &attr, NULL, 0, NULL, 0, &req);
1230                 if (rc) {
1231                         ptlrpc_req_finished(req);
1232                         if (rc != -EPERM && rc != -EACCES)
1233                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1234                         obdo_free(oa);
1235                         RETURN(rc);
1236                 }
1237                 ptlrpc_req_finished(req);
1238
1239                 oa->o_id = lsm->lsm_object_id;
1240                 oa->o_flags = flags;
1241                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
1242
1243                 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1244                 obdo_free(oa);
1245                 if (rc) {
1246                         if (rc != -EPERM && rc != -EACCES)
1247                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1248                         RETURN(rc);
1249                 }
1250
1251                 if (flags & EXT3_APPEND_FL)
1252                         inode->i_flags |= S_APPEND;
1253                 else
1254                         inode->i_flags &= ~S_APPEND;
1255                 if (flags & EXT3_IMMUTABLE_FL)
1256                         inode->i_flags |= S_IMMUTABLE;
1257                 else
1258                         inode->i_flags &= ~S_IMMUTABLE;
1259                 if (flags & EXT3_NOATIME_FL)
1260                         inode->i_flags |= S_NOATIME;
1261                 else
1262                         inode->i_flags &= ~S_NOATIME;
1263
1264                 RETURN(0);
1265         }
1266         default:
1267                 RETURN(-ENOSYS);
1268         }
1269
1270         RETURN(0);
1271 }
1272
1273 void ll_umount_begin(struct super_block *sb)
1274 {
1275         struct ll_sb_info *sbi = ll_s2sbi(sb);
1276         struct obd_device *obd;
1277         struct obd_ioctl_data ioc_data = { 0 };
1278         ENTRY;
1279         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1280                sb->s_count, atomic_read(&sb->s_active));
1281
1282         obd = class_exp2obd(sbi->ll_mdc_exp);
1283         if (obd == NULL) {
1284                 CERROR("Invalid MDC connection handle "LPX64"\n",
1285                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1286                 EXIT;
1287                 return;
1288         }
1289         obd->obd_no_recov = 1;
1290         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1291                       &ioc_data, NULL);
1292
1293         obd = class_exp2obd(sbi->ll_osc_exp);
1294         if (obd == NULL) {
1295                 CERROR("Invalid LOV connection handle "LPX64"\n",
1296                        sbi->ll_osc_exp->exp_handle.h_cookie);
1297                 EXIT;
1298                 return;
1299         }
1300
1301         obd->obd_no_recov = 1;
1302         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1303                       &ioc_data, NULL);
1304
1305         /* Really, we'd like to wait until there are no requests outstanding,
1306          * and then continue.  For now, we just invalidate the requests,
1307          * schedule, and hope.
1308          */
1309         schedule();
1310
1311         EXIT;
1312 }
1313
1314 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
1315 {
1316         struct ll_sb_info *sbi = ll_s2sbi(sb);
1317         int err;
1318         __u32 read_only;
1319  
1320         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1321                 read_only = *flags & MS_RDONLY;
1322                 err = obd_set_info(sbi->ll_mdc_exp, strlen("read-only"),
1323                                    "read-only", sizeof(read_only), &read_only);
1324                 if (err) {
1325                         CERROR("Failed to change the read-only flag during "
1326                                "remount: %d\n", err);
1327                         return err;
1328                 }
1329  
1330                 if (read_only)
1331                         sb->s_flags |= MS_RDONLY;
1332                 else
1333                         sb->s_flags &= ~MS_RDONLY;
1334         }
1335         return 0;
1336 }
1337
1338 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
1339                   struct ptlrpc_request *req, int offset,struct super_block *sb)
1340 {
1341         struct lustre_md md;
1342         int rc = 0;
1343
1344         rc = mdc_req2lustre_md(req, offset, exp, &md);
1345         if (rc)
1346                 RETURN(rc);
1347
1348         if (*inode) {
1349                 ll_update_inode(*inode, md.body, md.lsm);
1350         } else {
1351                 LASSERT(sb);
1352                 *inode = ll_iget(sb, md.body->ino, &md);
1353                 if (*inode == NULL || is_bad_inode(*inode)) {
1354                         /* free the lsm if we allocated one above */
1355                         if (md.lsm != NULL)
1356                                 obd_free_memmd(exp, &md.lsm);
1357                         rc = -ENOMEM;
1358                         CERROR("new_inode -fatal: rc %d\n", rc);
1359                 }
1360         }
1361
1362         RETURN(rc);
1363 }
1364
1365 char *llap_origins[] = {
1366         [LLAP_ORIGIN_UNKNOWN] = "--",
1367         [LLAP_ORIGIN_READPAGE] = "rp",
1368         [LLAP_ORIGIN_READAHEAD] = "ra",
1369         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
1370         [LLAP_ORIGIN_WRITEPAGE] = "wp",
1371 };
1372
1373 struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
1374                                              struct list_head *list)
1375 {
1376         struct ll_async_page *llap;
1377         struct list_head *pos;
1378
1379         list_for_each(pos, list) {
1380                 if (pos == &sbi->ll_pglist)
1381                         return NULL;
1382                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
1383                 if (llap->llap_page == NULL)
1384                         continue;
1385                 return llap;
1386         }
1387         LBUG();
1388         return NULL;
1389 }
1390
1391 EXPORT_SYMBOL(ll_fill_super);
1392 EXPORT_SYMBOL(ll_put_super);
1393 EXPORT_SYMBOL(ll_remount_fs);
1394 EXPORT_SYMBOL(ll_umount_begin);
1395