Whamcloud - gitweb
2e5e41fcfd6cdb0969848ece3136ce80fb5a44a6
[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, 2003 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_dlm.h>
34 #include <linux/lprocfs_status.h>
35 #include "llite_internal.h"
36
37 kmem_cache_t *ll_file_data_slab;
38
39 extern struct address_space_operations ll_aops;
40 extern struct address_space_operations ll_dir_aops;
41
42 #ifndef log2
43 #define log2(n) ffz(~(n))
44 #endif
45
46 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
47 {
48         struct ll_sb_info *sbi = NULL;
49         class_uuid_t uuid;
50         ENTRY;
51
52         OBD_ALLOC(sbi, sizeof(*sbi));
53         if (!sbi)
54                 RETURN(NULL);
55
56         spin_lock_init(&sbi->ll_lock);
57         INIT_LIST_HEAD(&sbi->ll_pglist);
58         sbi->ll_pglist_gen = 0;
59         sbi->ll_max_read_ahead_pages = SBI_DEFAULT_RA_MAX;
60         INIT_LIST_HEAD(&sbi->ll_conn_chain);
61         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
62         INIT_LIST_HEAD(&sbi->ll_mnt_list);
63         sema_init(&sbi->ll_gns_sem, 1);
64         init_completion(&sbi->ll_gns_completion);
65         sbi->ll_gns_state = LL_GNS_STATE_IDLE;
66         sbi->ll_gns_timer.data = (unsigned long)sbi;
67         sbi->ll_gns_timer.function = ll_gns_timer_callback;
68         init_timer(&sbi->ll_gns_timer);
69         INIT_LIST_HEAD(&sbi->ll_gns_sbi_head);
70
71         ll_set_sbi(sb, sbi);
72
73         generate_random_uuid(uuid);
74         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
75         RETURN(sbi);
76 }
77
78 void lustre_free_sbi(struct super_block *sb)
79 {
80         struct ll_sb_info *sbi = ll_s2sbi(sb);
81         ENTRY;
82
83         if (sbi != NULL) {
84                 list_del(&sbi->ll_gns_sbi_head);
85                 del_timer(&sbi->ll_gns_timer);
86                 OBD_FREE(sbi, sizeof(*sbi));
87         }
88         ll_set_sbi(sb, NULL);
89         EXIT;
90 }
91
92 int lustre_init_ea_size(struct ll_sb_info *sbi)
93 {
94         struct lov_desc desc;
95         int rc, valsize;
96         
97         /* OSC may have larger ea size */
98         valsize = sizeof(desc);
99         rc = obd_get_info(sbi->ll_osc_exp, strlen("lovdesc") + 1, "lovdesc", 
100                           &valsize, &desc);
101         if (rc == 0) {
102                 obd_init_ea_size(sbi->ll_mdc_exp,
103                                  obd_size_diskmd(sbi->ll_osc_exp, NULL),
104                                  desc.ld_tgt_count*sizeof(struct llog_cookie));
105
106                 /* declare ourself as real client. not connection
107                  * from another MDS 
108                  * FIXME: remove fake valsize, mdsize --bzzz */
109                 rc = obd_set_info(sbi->ll_mdc_exp, strlen("client"),
110                                   "client", valsize, &desc);
111         }
112         RETURN(rc);
113 }
114
115 int lustre_common_fill_super(struct super_block *sb, char *mdc, char *osc)
116 {
117         struct inode *root = 0;
118         struct ll_sb_info *sbi = ll_s2sbi(sb);
119         struct obd_device *obd;
120         struct ll_fid rootfid;
121         struct obd_statfs osfs;
122         struct ptlrpc_request *request = NULL;
123         struct lustre_handle osc_conn = {0, };
124         struct lustre_handle mdc_conn = {0, };
125         struct lustre_md md;
126         kdev_t devno;
127         int err;
128
129         obd = class_name2obd(mdc);
130         if (!obd) {
131                 CERROR("MDC %s: not setup or attached\n", mdc);
132                 RETURN(-EINVAL);
133         }
134
135         if (proc_lustre_fs_root) {
136                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
137                                                   osc, mdc);
138                 if (err < 0)
139                         CERROR("could not register mount in /proc/lustre");
140         }
141
142         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid);
143         if (err == -EBUSY) {
144                 CERROR("An MDS (mdc %s) is performing recovery, of which this"
145                        " client is not a part.  Please wait for recovery to "
146                        "complete, abort, or time out.\n", mdc);
147                 GOTO(out, err);
148         } else if (err) {
149                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
150                 GOTO(out, err);
151         }
152         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
153
154         err = obd_statfs(obd, &osfs, jiffies - HZ);
155         if (err)
156                 GOTO(out_mdc, err);
157
158         LASSERT(osfs.os_bsize);
159         sb->s_blocksize = osfs.os_bsize;
160         sb->s_blocksize_bits = log2(osfs.os_bsize);
161         sb->s_magic = LL_SUPER_MAGIC;
162         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
163        
164         devno = get_uuid2int(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid, 
165                              strlen(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid));
166
167         sb->s_dev = devno;
168
169         obd = class_name2obd(osc);
170         if (!obd) {
171                 CERROR("OSC %s: not setup or attached\n", osc);
172                 GOTO(out_mdc, err);
173         }
174
175         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid);
176         if (err == -EBUSY) {
177                 CERROR("An OST (osc %s) is performing recovery, of which this"
178                        " client is not a part.  Please wait for recovery to "
179                        "complete, abort, or time out.\n", osc);
180                 GOTO(out, err);
181         } else if (err) {
182                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
183                 GOTO(out_mdc, err);
184         }
185         sbi->ll_osc_exp = class_conn2export(&osc_conn);
186
187         lustre_init_ea_size(sbi);
188
189         err = md_getstatus(sbi->ll_mdc_exp, &rootfid);
190         if (err) {
191                 CERROR("cannot mds_connect: rc = %d\n", err);
192                 GOTO(out_osc, err);
193         }
194         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
195         sbi->ll_rootino = rootfid.id;
196
197         sb->s_op = &lustre_super_operations;
198
199         /* make root inode
200          * XXX: move this to after cbd setup? */
201         err = md_getattr(sbi->ll_mdc_exp, &rootfid,
202                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS, 0, &request);
203         if (err) {
204                 CERROR("md_getattr failed for root: rc = %d\n", err);
205                 GOTO(out_osc, err);
206         }
207
208         err = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0, 
209                                 sbi->ll_osc_exp, &md);
210         if (err) {
211                 CERROR("failed to understand root inode md: rc = %d\n",err);
212                 ptlrpc_req_finished (request);
213                 GOTO(out_osc, err);
214         }
215
216         LASSERT(sbi->ll_rootino != 0);
217         root = ll_iget(sb, sbi->ll_rootino, &md);
218
219         ptlrpc_req_finished(request);
220
221         if (root == NULL || is_bad_inode(root)) {
222                 /* XXX might need iput() for bad inode */
223                 CERROR("lustre_lite: bad iget4 for root\n");
224                 GOTO(out_root, err = -EBADF);
225         }
226
227         err = ll_close_thread_start(&sbi->ll_lcq);
228         if (err) {
229                 CERROR("cannot start close thread: rc %d\n", err);
230                 GOTO(out_root, err);
231         }
232
233         ll_gns_add_timer(sbi);
234
235         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
236            backing dev info assigned to inode mapping is used for
237            determining maximal readahead. */
238 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
239         /* bug 2805 - set VM readahead to zero */
240         vm_max_readahead = vm_min_readahead = 0;
241 #endif
242
243         sb->s_root = d_alloc_root(root);
244         RETURN(err);
245
246 out_root:
247         if (root)
248                 iput(root);
249 out_osc:
250         obd_disconnect(sbi->ll_osc_exp, 0);
251 out_mdc:
252         obd_disconnect(sbi->ll_mdc_exp, 0);
253 out:
254         lprocfs_unregister_mountpoint(sbi);
255         RETURN(err);
256 }
257
258 void lustre_common_put_super(struct super_block *sb)
259 {
260         struct ll_sb_info *sbi = ll_s2sbi(sb);
261         struct hlist_node *tmp, *next;
262         ENTRY;
263
264         ll_gns_del_timer(sbi);
265
266         ll_close_thread_shutdown(sbi->ll_lcq);
267
268         list_del(&sbi->ll_conn_chain);
269         obd_disconnect(sbi->ll_osc_exp, 0);
270
271         lprocfs_unregister_mountpoint(sbi);
272         if (sbi->ll_proc_root) {
273                 lprocfs_remove(sbi->ll_proc_root);
274                 sbi->ll_proc_root = NULL;
275         }
276
277         obd_disconnect(sbi->ll_mdc_exp, 0);
278
279         // We do this to get rid of orphaned dentries. That is not really trw.
280         spin_lock(&dcache_lock);
281         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
282                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
283                 CWARN("orphan dentry %*s (%p) at unmount\n",
284                       dentry->d_name.len, dentry->d_name.name, dentry);
285                 shrink_dcache_parent(dentry);
286         }
287         spin_unlock(&dcache_lock);
288         EXIT;
289 }
290
291
292 char *ll_read_opt(const char *opt, char *data)
293 {
294         char *value;
295         char *retval;
296         ENTRY;
297
298         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
299         if (strncmp(opt, data, strlen(opt)))
300                 RETURN(NULL);
301         if ((value = strchr(data, '=')) == NULL)
302                 RETURN(NULL);
303
304         value++;
305         OBD_ALLOC(retval, strlen(value) + 1);
306         if (!retval) {
307                 CERROR("out of memory!\n");
308                 RETURN(NULL);
309         }
310
311         memcpy(retval, value, strlen(value)+1);
312         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
313         RETURN(retval);
314 }
315
316 int ll_set_opt(const char *opt, char *data, int fl)
317 {
318         ENTRY;
319
320         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
321         if (strncmp(opt, data, strlen(opt)))
322                 RETURN(0);
323         else
324                 RETURN(fl);
325 }
326
327 void ll_options(char *options, char **ost, char **mdc, int *flags)
328 {
329         char *this_char;
330 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
331         char *opt_ptr = options;
332 #endif
333         ENTRY;
334
335         if (!options) {
336                 EXIT;
337                 return;
338         }
339
340 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
341         for (this_char = strtok (options, ",");
342              this_char != NULL;
343              this_char = strtok (NULL, ",")) {
344 #else
345         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
346 #endif
347                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
348                 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
349                         continue;
350                 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
351                         continue;
352                 if (!(*flags & LL_SBI_NOLCK) &&
353                     ((*flags) = (*flags) |
354                                 ll_set_opt("nolock", this_char,
355                                            LL_SBI_NOLCK)))
356                         continue;
357         }
358         EXIT;
359 }
360
361 void ll_lli_init(struct ll_inode_info *lli)
362 {
363         sema_init(&lli->lli_open_sem, 1);
364         lli->lli_flags = 0;
365         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
366         spin_lock_init(&lli->lli_lock);
367         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
368 }
369
370 int ll_fill_super(struct super_block *sb, void *data, int silent)
371 {
372         struct ll_sb_info *sbi;
373         char *osc = NULL;
374         char *mdc = NULL;
375         int err;
376         ENTRY;
377
378         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
379
380         sbi = lustre_init_sbi(sb);
381         if (!sbi)
382                 RETURN(-ENOMEM);
383
384         sbi->ll_flags |= LL_SBI_READAHEAD;
385         ll_options(data, &osc, &mdc, &sbi->ll_flags);
386
387         if (!osc) {
388                 CERROR("no osc\n");
389                 GOTO(out, err = -EINVAL);
390         }
391
392         if (!mdc) {
393                 CERROR("no mdc\n");
394                 GOTO(out, err = -EINVAL);
395         }
396
397         err = lustre_common_fill_super(sb, mdc, osc);
398 out:
399         if (err)
400                 lustre_free_sbi(sb);
401
402         if (mdc)
403                 OBD_FREE(mdc, strlen(mdc) + 1);
404         if (osc)
405                 OBD_FREE(osc, strlen(osc) + 1);
406
407         RETURN(err);
408 } /* ll_read_super */
409
410 int lustre_process_log(struct lustre_mount_data *lmd, char * profile,
411                        struct config_llog_instance *cfg, int allow_recov)
412 {
413         struct lustre_cfg lcfg;
414         struct portals_cfg pcfg;
415         char * peer = "MDS_PEER_UUID";
416         struct obd_device *obd;
417         struct lustre_handle mdc_conn = {0, };
418         struct obd_export *exp;
419         char * name = "mdc_dev";
420         class_uuid_t uuid;
421         struct obd_uuid mdc_uuid;
422         struct llog_ctxt *ctxt;
423         int rc = 0;
424         int err;
425         ENTRY;
426
427         if (lmd_bad_magic(lmd))
428                 RETURN(-EINVAL);
429
430         generate_random_uuid(uuid);
431         class_uuid_unparse(uuid, &mdc_uuid);
432
433         if (lmd->lmd_local_nid) {
434                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
435                 pcfg.pcfg_nal = lmd->lmd_nal;
436                 pcfg.pcfg_nid = lmd->lmd_local_nid;
437                 err = libcfs_nal_cmd(&pcfg);
438                 if (err <0)
439                         GOTO(out, err);
440         }
441
442         if (lmd->lmd_nal == SOCKNAL) {
443                 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
444                 pcfg.pcfg_nal     = lmd->lmd_nal;
445                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
446                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
447                 pcfg.pcfg_misc    = lmd->lmd_port;
448                 pcfg.pcfg_size    = 8388608;
449                 pcfg.pcfg_flags   = 0x4; /*share*/
450                 err = libcfs_nal_cmd(&pcfg);
451                 if (err <0)
452                         GOTO(out, err);
453         }
454
455         LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
456         lcfg.lcfg_nid = lmd->lmd_server_nid;
457         lcfg.lcfg_inllen1 = strlen(peer) + 1;
458         lcfg.lcfg_inlbuf1 = peer;
459         lcfg.lcfg_nal = lmd->lmd_nal;
460         err = class_process_config(&lcfg);
461         if (err < 0)
462                 GOTO(out_del_conn, err);
463
464         LCFG_INIT(lcfg, LCFG_ATTACH, name);
465         lcfg.lcfg_inlbuf1 = "mdc";
466         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
467         lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
468         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
469         err = class_process_config(&lcfg);
470         if (err < 0)
471                 GOTO(out_del_uuid, err);
472
473         LCFG_INIT(lcfg, LCFG_SETUP, name);
474         lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
475         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
476         lcfg.lcfg_inlbuf2 = peer;
477         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
478         err = class_process_config(&lcfg);
479         if (err < 0)
480                 GOTO(out_detach, err);
481
482         obd = class_name2obd(name);
483         if (obd == NULL)
484                 GOTO(out_cleanup, err = -EINVAL);
485
486         /* Disable initial recovery on this import */
487         err = obd_set_info(obd->obd_self_export,
488                            strlen("initial_recov"), "initial_recov",
489                            sizeof(allow_recov), &allow_recov);
490         if (err)
491                 GOTO(out_cleanup, err);
492
493         err = obd_connect(&mdc_conn, obd, &mdc_uuid);
494         if (err) {
495                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
496                 GOTO(out_cleanup, err);
497         }
498
499         exp = class_conn2export(&mdc_conn);
500
501         ctxt = llog_get_context(&exp->exp_obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
502 #if 1
503         rc = class_config_parse_llog(ctxt, profile, cfg);
504 #else
505         /*
506          * For debugging, it's useful to just dump the log
507          */
508         rc = class_config_dump_llog(ctxt, profile, cfg);
509 #endif
510         if (rc) {
511                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
512         }
513
514         err = obd_disconnect(exp, 0);
515
516 out_cleanup:
517         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
518         err = class_process_config(&lcfg);
519         if (err < 0)
520                 GOTO(out, err);
521
522 out_detach:
523         LCFG_INIT(lcfg, LCFG_DETACH, name);
524         err = class_process_config(&lcfg);
525         if (err < 0)
526                 GOTO(out, err);
527
528 out_del_uuid:
529         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
530         lcfg.lcfg_inllen1 = strlen(peer) + 1;
531         lcfg.lcfg_inlbuf1 = peer;
532         err = class_process_config(&lcfg);
533
534 out_del_conn:
535         if (lmd->lmd_nal == SOCKNAL) {
536                 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
537                 pcfg.pcfg_nal     = lmd->lmd_nal;
538                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
539                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
540                 pcfg.pcfg_flags   = 1; /*share*/
541                 err = libcfs_nal_cmd(&pcfg);
542                 if (err <0)
543                         GOTO(out, err);
544         }
545 out:
546         if (rc == 0)
547                 rc = err;
548
549         RETURN(rc);
550 }
551
552 int lustre_fill_super(struct super_block *sb, void *data, int silent)
553 {
554         struct lustre_mount_data * lmd = data;
555         struct ll_sb_info *sbi;
556         char *osc = NULL;
557         char *mdc = NULL;
558         int err;
559         ENTRY;
560
561         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
562         if (lmd_bad_magic(lmd))
563                 RETURN(-EINVAL);
564
565         sbi = lustre_init_sbi(sb);
566         if (!sbi)
567                 RETURN(-ENOMEM);
568
569         sbi->ll_flags |= LL_SBI_READAHEAD;
570
571         if (lmd->lmd_profile) {
572                 struct lustre_profile *lprof;
573                 struct config_llog_instance cfg;
574                 int len;
575
576                 if (lmd->lmd_mds[0] == '\0') {
577                         CERROR("no mds name\n");
578                         GOTO(out_free, err = -EINVAL);
579                 }
580
581                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
582                 if (sbi->ll_lmd == NULL)
583                         GOTO(out_free, err = -ENOMEM);
584                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
585
586                 /* generate a string unique to this super, let's try
587                  the address of the super itself.*/
588                 len = (sizeof(sb) * 2) + 1;
589                 OBD_ALLOC(sbi->ll_instance, len);
590                 if (sbi->ll_instance == NULL)
591                         GOTO(out_free, err = -ENOMEM);
592                 sprintf(sbi->ll_instance, "%p", sb);
593
594                 cfg.cfg_instance = sbi->ll_instance;
595                 cfg.cfg_uuid = sbi->ll_sb_uuid;
596                 cfg.cfg_local_nid = lmd->lmd_local_nid;
597                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
598                 if (err < 0) {
599                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
600
601                         GOTO(out_free, err);
602                 }
603
604                 lprof = class_get_profile(lmd->lmd_profile);
605                 if (lprof == NULL) {
606                         CERROR("No profile found: %s\n", lmd->lmd_profile);
607                         GOTO(out_free, err = -EINVAL);
608                 }
609                 if (osc)
610                         OBD_FREE(osc, strlen(osc) + 1);
611                 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
612                           strlen(sbi->ll_instance) + 2);
613                 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
614
615                 if (mdc)
616                         OBD_FREE(mdc, strlen(mdc) + 1);
617                 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
618                           strlen(sbi->ll_instance) + 2);
619                 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
620         }
621
622         if (!osc) {
623                 CERROR("no osc\n");
624                 GOTO(out_free, err = -EINVAL);
625         }
626
627         if (!mdc) {
628                 CERROR("no mdc\n");
629                 GOTO(out_free, err = -EINVAL);
630         }
631
632         err = lustre_common_fill_super(sb, mdc, osc);
633
634         if (err)
635                 GOTO(out_free, err);
636
637 out_dev:
638         if (mdc)
639                 OBD_FREE(mdc, strlen(mdc) + 1);
640         if (osc)
641                 OBD_FREE(osc, strlen(osc) + 1);
642
643         RETURN(err);
644
645 out_free:
646         if (sbi->ll_lmd) {
647                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
648                 int err;
649
650                 if (sbi->ll_instance != NULL) {
651                         char * cln_prof;
652                         struct config_llog_instance cfg;
653
654                         cfg.cfg_instance = sbi->ll_instance;
655                         cfg.cfg_uuid = sbi->ll_sb_uuid;
656
657                         OBD_ALLOC(cln_prof, len);
658                         sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
659
660                         err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg,
661                                                  0);
662                         if (err < 0)
663                                 CERROR("Unable to process log: %s\n", cln_prof);
664                         OBD_FREE(cln_prof, len);
665                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
666                 }
667                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
668         }
669         lustre_free_sbi(sb);
670
671         goto out_dev;
672 } /* lustre_fill_super */
673
674 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
675 {
676         struct lustre_cfg lcfg;
677         struct obd_device *obd;
678         int next = 0;
679
680         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
681         {
682                 int err;
683
684                 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
685                 err = class_process_config(&lcfg);
686                 if (err) {
687                         CERROR("cleanup failed: %s\n", obd->obd_name);
688                         //continue;
689                 }
690
691                 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
692                 err = class_process_config(&lcfg);
693                 if (err) {
694                         CERROR("detach failed: %s\n", obd->obd_name);
695                         //continue;
696                 }
697         }
698
699         if (sbi->ll_lmd != NULL)
700                 class_del_profile(sbi->ll_lmd->lmd_profile);
701 }
702
703 void lustre_put_super(struct super_block *sb)
704 {
705         struct obd_device *obd;
706         struct ll_sb_info *sbi = ll_s2sbi(sb);
707         int force_umount = 0;
708         ENTRY;
709
710         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
711         obd = class_exp2obd(sbi->ll_mdc_exp);
712         if (obd)
713                 force_umount = obd->obd_no_recov;
714         obd = NULL;
715
716         lustre_common_put_super(sb);
717
718         if (sbi->ll_lmd != NULL) {
719                 char * cln_prof;
720                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
721                 int err;
722                 struct config_llog_instance cfg;
723
724                 if (force_umount) {
725                         CERROR("force umount, doing manual cleanup\n");
726                         lustre_manual_cleanup(sbi);
727                         GOTO(free_lmd, 0);
728                 }
729
730                 cfg.cfg_instance = sbi->ll_instance;
731                 cfg.cfg_uuid = sbi->ll_sb_uuid;
732
733                 OBD_ALLOC(cln_prof, len);
734                 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
735
736                 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
737                 if (err < 0) {
738                         CERROR("Unable to process log: %s, doing manual cleanup"
739                                "\n", cln_prof);
740                         lustre_manual_cleanup(sbi);
741                 }
742
743                 OBD_FREE(cln_prof, len);
744         free_lmd:
745                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
746                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
747         }
748
749         lustre_free_sbi(sb);
750
751         EXIT;
752 } /* lustre_put_super */
753
754
755 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
756 {
757         struct inode *inode;
758         l_lock(&lock->l_resource->lr_namespace->ns_lock);
759         if (lock->l_ast_data)
760                 inode = igrab(lock->l_ast_data);
761         else
762                 inode = NULL;
763         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
764         return inode;
765 }
766
767 int null_if_equal(struct ldlm_lock *lock, void *data)
768 {
769         if (data == lock->l_ast_data)
770                 lock->l_ast_data = NULL;
771
772         if (lock->l_req_mode != lock->l_granted_mode)
773                 return LDLM_ITER_STOP;
774
775         return LDLM_ITER_CONTINUE;
776 }
777
778 void ll_clear_inode(struct inode *inode)
779 {
780         struct ll_fid fid;
781         struct ll_inode_info *lli = ll_i2info(inode);
782         struct ll_sb_info *sbi = ll_i2sbi(inode);
783         ENTRY;
784
785         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
786                inode->i_generation, inode);
787
788         ll_inode2fid(&fid, inode);
789         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
790         md_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
791
792         if (lli->lli_smd)
793                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
794                                   null_if_equal, inode);
795
796         if (lli->lli_smd) {
797                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
798                 lli->lli_smd = NULL;
799         }
800
801         if (lli->lli_mea) {
802                 /* FIXME: change cbdata for mea here */
803                 obd_free_memmd(sbi->ll_mdc_exp,
804                                (struct lov_stripe_md **) &lli->lli_mea);
805                 lli->lli_mea = NULL;
806         }
807
808         if (lli->lli_symlink_name) {
809                 OBD_FREE(lli->lli_symlink_name,
810                          strlen(lli->lli_symlink_name) + 1);
811                 lli->lli_symlink_name = NULL;
812         }
813
814         EXIT;
815 }
816
817 /* If this inode has objects allocated to it (lsm != NULL), then the OST
818  * object(s) determine the file size and mtime.  Otherwise, the MDS will
819  * keep these values until such a time that objects are allocated for it.
820  * We do the MDS operations first, as it is checking permissions for us.
821  * We don't to the MDS RPC if there is nothing that we want to store there,
822  * otherwise there is no harm in updating mtime/atime on the MDS if we are
823  * going to do an RPC anyways.
824  *
825  * If we are doing a truncate, we will send the mtime and ctime updates
826  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
827  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
828  * at the same time.
829  */
830 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
831 {
832         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
833         struct ll_sb_info *sbi = ll_i2sbi(inode);
834         struct ptlrpc_request *request = NULL;
835         struct mdc_op_data op_data;
836         int ia_valid = attr->ia_valid;
837         int rc = 0;
838         ENTRY;
839
840         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
841         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
842
843         if (ia_valid & ATTR_SIZE) {
844                 if (attr->ia_size > ll_file_maxbytes(inode)) {
845                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
846                                attr->ia_size, ll_file_maxbytes(inode));
847                         RETURN(-EFBIG);
848                 }
849
850                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
851         }
852
853         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
854         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
855                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
856                         RETURN(-EPERM);
857         }
858
859         /* We mark all of the fields "set" so MDS/OST does not re-set them */
860         if (attr->ia_valid & ATTR_CTIME) {
861                 attr->ia_ctime = CURRENT_TIME;
862                 attr->ia_valid |= ATTR_CTIME_SET;
863         }
864         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
865                 attr->ia_atime = CURRENT_TIME;
866                 attr->ia_valid |= ATTR_ATIME_SET;
867         }
868         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
869                 attr->ia_mtime = CURRENT_TIME;
870                 attr->ia_valid |= ATTR_MTIME_SET;
871         }
872
873         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
874                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
875                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
876                        LTIME_S(CURRENT_TIME));
877         if (lsm)
878                 attr->ia_valid &= ~ATTR_SIZE;
879
880         /* If only OST attributes being set on objects, don't do MDS RPC.
881          * In that case, we need to check permissions and update the local
882          * inode ourselves so we can call obdo_from_inode() always. */
883         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
884                 struct lustre_md md;
885                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
886
887                 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
888                                 attr, NULL, 0, NULL, 0, &request);
889                 if (rc) {
890                         ptlrpc_req_finished(request);
891                         if (rc != -EPERM && rc != -EACCES)
892                                 CERROR("md_setattr fails: rc = %d\n", rc);
893                         RETURN(rc);
894                 }
895
896                 rc = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0, 
897                                        sbi->ll_osc_exp, &md);
898                 if (rc) {
899                         ptlrpc_req_finished(request);
900                         RETURN(rc);
901                 }
902
903                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
904                  * but needed to set timestamps backwards on utime. */
905                 inode_setattr(inode, attr);
906                 ll_update_inode(inode, &md);
907                 ptlrpc_req_finished(request);
908
909                 if (!lsm || !S_ISREG(inode->i_mode)) {
910                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
911                         RETURN(0);
912                 }
913         } else {
914                 /* The OST doesn't check permissions, but the alternative is
915                  * a gratuitous RPC to the MDS.  We already rely on the client
916                  * to do read/write/truncate permission checks, so is mtime OK?
917                  */
918                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
919                         /* from sys_utime() */
920                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
921                                 if (current->fsuid != inode->i_uid &&
922                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
923                                         RETURN(rc);
924                         } else {
925                                 /* from inode_change_ok() */
926                                 if (current->fsuid != inode->i_uid &&
927                                     !capable(CAP_FOWNER))
928                                         RETURN(-EPERM);
929                         }
930                 }
931
932                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
933                 inode_setattr(inode, attr);
934         }
935
936         /* We really need to get our PW lock before we change inode->i_size.
937          * If we don't we can race with other i_size updaters on our node, like
938          * ll_file_read.  We can also race with i_size propogation to other
939          * nodes through dirtying and writeback of final cached pages.  This
940          * last one is especially bad for racing o_append users on other
941          * nodes. */
942         if (ia_valid & ATTR_SIZE) {
943                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
944                                                            OBD_OBJECT_EOF } };
945                 struct lustre_handle lockh = { 0 };
946                 int err, ast_flags = 0;
947                 /* XXX when we fix the AST intents to pass the discard-range
948                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
949                  * XXX here. */
950                 if (attr->ia_size == 0)
951                         ast_flags = LDLM_AST_DISCARD_DATA;
952
953                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
954                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
955                 up(&inode->i_sem);
956                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
957                                     ast_flags);
958                 down(&inode->i_sem);
959                 if (rc != 0)
960                         RETURN(rc);
961
962                 rc = vmtruncate(inode, attr->ia_size);
963
964                 /* We need to drop the semaphore here, because this unlock may
965                  * result in a cancellation, which will need the i_sem */
966                 up(&inode->i_sem);
967                 /* unlock now as we don't mind others file lockers racing with
968                  * the mds updates below? */
969                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
970                 down(&inode->i_sem);
971                 if (err) {
972                         CERROR("ll_extent_unlock failed: %d\n", err);
973                         if (!rc)
974                                 rc = err;
975                 }
976         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
977                 struct obdo oa;
978
979                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
980                        inode->i_ino, LTIME_S(attr->ia_mtime));
981                 oa.o_id = lsm->lsm_object_id;
982                 oa.o_gr = lsm->lsm_object_gr;
983                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
984                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
985                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
986                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
987                 if (rc)
988                         CERROR("obd_setattr fails: rc=%d\n", rc);
989         }
990         RETURN(rc);
991 }
992
993 int ll_setattr(struct dentry *de, struct iattr *attr)
994 {
995         LBUG(); /* code is unused, but leave this in case of VFS changes */
996         RETURN(-ENOSYS);
997 }
998
999 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1000                        unsigned long max_age)
1001 {
1002         struct ll_sb_info *sbi = ll_s2sbi(sb);
1003         struct obd_statfs obd_osfs;
1004         int rc;
1005         ENTRY;
1006
1007         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1008         if (rc) {
1009                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1010                 RETURN(rc);
1011         }
1012
1013         osfs->os_type = sb->s_magic;
1014
1015         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1016                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1017
1018         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1019         if (rc) {
1020                 CERROR("obd_statfs fails: rc = %d\n", rc);
1021                 RETURN(rc);
1022         }
1023
1024         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1025                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1026                obd_osfs.os_files);
1027
1028         osfs->os_blocks = obd_osfs.os_blocks;
1029         osfs->os_bfree = obd_osfs.os_bfree;
1030         osfs->os_bavail = obd_osfs.os_bavail;
1031
1032         /* If we don't have as many objects free on the OST as inodes
1033          * on the MDS, we reduce the total number of inodes to
1034          * compensate, so that the "inodes in use" number is correct.
1035          */
1036         if (obd_osfs.os_ffree < osfs->os_ffree) {
1037                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1038                         obd_osfs.os_ffree;
1039                 osfs->os_ffree = obd_osfs.os_ffree;
1040         }
1041
1042         RETURN(rc);
1043 }
1044
1045 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1046 {
1047         struct obd_statfs osfs;
1048         int rc;
1049
1050         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1051         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1052
1053         /* For now we will always get up-to-date statfs values, but in the
1054          * future we may allow some amount of caching on the client (e.g.
1055          * from QOS or lprocfs updates). */
1056         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1057         if (rc)
1058                 return rc;
1059
1060         statfs_unpack(sfs, &osfs);
1061
1062         if (sizeof(sfs->f_blocks) == 4) {
1063                 while (osfs.os_blocks > ~0UL) {
1064                         sfs->f_bsize <<= 1;
1065
1066                         osfs.os_blocks >>= 1;
1067                         osfs.os_bfree >>= 1;
1068                         osfs.os_bavail >>= 1;
1069                 }
1070         }
1071
1072         sfs->f_blocks = osfs.os_blocks;
1073         sfs->f_bfree = osfs.os_bfree;
1074         sfs->f_bavail = osfs.os_bavail;
1075
1076         return 0;
1077 }
1078
1079 void dump_lsm(int level, struct lov_stripe_md *lsm)
1080 {
1081         CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
1082                "stripe_size %u, stripe_count %u\n",
1083                lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
1084                lsm->lsm_stripe_size, lsm->lsm_stripe_count);
1085 }
1086
1087 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1088 {
1089         struct ll_inode_info *lli = ll_i2info(inode);
1090         struct mds_body *body = md->body;
1091         struct lov_stripe_md *lsm = md->lsm;
1092         struct mea *mea = md->mea;
1093         ENTRY;
1094
1095         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1096         LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1097         if (lsm != NULL) {
1098                 LASSERT(lsm->lsm_object_gr > 0);
1099                 if (lli->lli_smd == NULL) {
1100                         lli->lli_smd = lsm;
1101                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1102                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1103                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1104                 } else {
1105                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1106                                 CERROR("lsm mismatch for inode %ld\n",
1107                                        inode->i_ino);
1108                                 CERROR("lli_smd:\n");
1109                                 dump_lsm(D_ERROR, lli->lli_smd);
1110                                 CERROR("lsm:\n");
1111                                 dump_lsm(D_ERROR, lsm);
1112                                 LBUG();
1113                         }
1114                 }
1115                 /* bug 2844 - limit i_blksize for broken user-space apps */
1116                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1117                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1118                 if (lli->lli_smd != lsm)
1119                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1120         }
1121
1122         if (mea != NULL) {
1123                 if (lli->lli_mea == NULL) {
1124                         lli->lli_mea = mea;
1125                 } else {
1126                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1127                                 CERROR("mea mismatch for inode %lu\n",
1128                                         inode->i_ino);
1129                                 LBUG();
1130                         }
1131                 }
1132                 if (lli->lli_mea != mea)
1133                         obd_free_memmd(ll_i2mdcexp(inode),
1134                                         (struct lov_stripe_md **) &mea);
1135                         
1136         }
1137
1138         if (body->valid & OBD_MD_FLID)
1139                 inode->i_ino = body->ino;
1140         if (body->valid & OBD_MD_FLATIME)
1141                 LTIME_S(inode->i_atime) = body->atime;
1142         if (body->valid & OBD_MD_FLMTIME &&
1143             body->mtime > LTIME_S(inode->i_mtime)) {
1144                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1145                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1146                 LTIME_S(inode->i_mtime) = body->mtime;
1147         }
1148         if (body->valid & OBD_MD_FLCTIME &&
1149             body->ctime > LTIME_S(inode->i_ctime))
1150                 LTIME_S(inode->i_ctime) = body->ctime;
1151         if (body->valid & OBD_MD_FLMODE)
1152                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1153         if (body->valid & OBD_MD_FLTYPE)
1154                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1155         if (body->valid & OBD_MD_FLUID)
1156                 inode->i_uid = body->uid;
1157         if (body->valid & OBD_MD_FLGID)
1158                 inode->i_gid = body->gid;
1159         if (body->valid & OBD_MD_FLFLAGS)
1160                 inode->i_flags = body->flags;
1161         if (body->valid & OBD_MD_FLNLINK)
1162                 inode->i_nlink = body->nlink;
1163         if (body->valid & OBD_MD_FLGENER)
1164                 inode->i_generation = body->generation;
1165         if (body->valid & OBD_MD_FLRDEV)
1166 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1167                 inode->i_rdev = body->rdev;
1168 #else
1169                 inode->i_rdev = old_decode_dev(body->rdev);
1170 #endif
1171         if (body->valid & OBD_MD_FLSIZE)
1172                 inode->i_size = body->size;
1173         if (body->valid & OBD_MD_FLBLOCKS)
1174                 inode->i_blocks = body->blocks;
1175
1176         if (body->valid & OBD_MD_FLSIZE)
1177                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1178
1179         lli->lli_mds = body->mds;
1180         inode->i_dev = (kdev_t) body->mds;
1181         LASSERT(body->mds < 1000);
1182 }
1183
1184 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1185 static struct backing_dev_info ll_backing_dev_info = {
1186         .ra_pages       = 0,    /* No readahead */
1187         .memory_backed  = 0,    /* Does contribute to dirty memory */
1188 };
1189 #endif
1190
1191 void ll_read_inode2(struct inode *inode, void *opaque)
1192 {
1193         struct lustre_md *md = opaque;
1194         struct ll_inode_info *lli = ll_i2info(inode);
1195         ENTRY;
1196
1197         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1198                inode->i_generation, inode);
1199
1200         ll_lli_init(lli);
1201
1202         LASSERT(!lli->lli_smd);
1203
1204         /* Core attributes from the MDS first.  This is a new inode, and
1205          * the VFS doesn't zero times in the core inode so we have to do
1206          * it ourselves.  They will be overwritten by either MDS or OST
1207          * attributes - we just need to make sure they aren't newer. */
1208         LTIME_S(inode->i_mtime) = 0;
1209         LTIME_S(inode->i_atime) = 0;
1210         LTIME_S(inode->i_ctime) = 0;
1211
1212         inode->i_rdev = 0;
1213         ll_update_inode(inode, md);
1214
1215         /* OIDEBUG(inode); */
1216
1217         if (S_ISREG(inode->i_mode)) {
1218                 inode->i_op = &ll_file_inode_operations;
1219                 inode->i_fop = &ll_file_operations;
1220                 inode->i_mapping->a_ops = &ll_aops;
1221                 EXIT;
1222         } else if (S_ISDIR(inode->i_mode)) {
1223                 inode->i_op = &ll_dir_inode_operations;
1224                 inode->i_fop = &ll_dir_operations;
1225                 inode->i_mapping->a_ops = &ll_dir_aops;
1226                 EXIT;
1227         } else if (S_ISLNK(inode->i_mode)) {
1228                 inode->i_op = &ll_fast_symlink_inode_operations;
1229                 EXIT;
1230         } else {
1231                 inode->i_op = &ll_special_inode_operations;
1232
1233 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1234                 init_special_inode(inode, inode->i_mode,
1235                                    kdev_t_to_nr(inode->i_rdev));
1236
1237                 /* initializing backing dev info. */
1238                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1239 #else
1240                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1241 #endif
1242                 lli->ll_save_ifop = inode->i_fop;
1243
1244                 if (S_ISCHR(inode->i_mode))
1245                         inode->i_fop = &ll_special_chr_inode_fops;
1246                 else if (S_ISBLK(inode->i_mode))
1247                         inode->i_fop = &ll_special_blk_inode_fops;
1248                 else if (S_ISFIFO(inode->i_mode))
1249                         inode->i_fop = &ll_special_fifo_inode_fops;
1250                 else if (S_ISSOCK(inode->i_mode))
1251                         inode->i_fop = &ll_special_sock_inode_fops;
1252
1253                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1254                       inode->i_fop);
1255
1256                 if (lli->ll_save_ifop->owner) {
1257                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
1258                               lli->ll_save_ifop->owner);
1259                 }
1260                 EXIT;
1261         }
1262 }
1263
1264 int ll_iocontrol(struct inode *inode, struct file *file,
1265                  unsigned int cmd, unsigned long arg)
1266 {
1267         struct ll_sb_info *sbi = ll_i2sbi(inode);
1268         struct ptlrpc_request *req = NULL;
1269         int rc, flags = 0;
1270         ENTRY;
1271
1272         switch(cmd) {
1273         case EXT3_IOC_GETFLAGS: {
1274                 struct ll_fid fid;
1275                 unsigned long valid = OBD_MD_FLFLAGS;
1276                 struct mds_body *body;
1277
1278                 ll_inode2fid(&fid, inode);
1279                 rc = md_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1280                 if (rc) {
1281                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1282                         RETURN(-abs(rc));
1283                 }
1284
1285                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1286
1287                 if (body->flags & S_APPEND)
1288                         flags |= EXT3_APPEND_FL;
1289                 if (body->flags & S_IMMUTABLE)
1290                         flags |= EXT3_IMMUTABLE_FL;
1291                 if (body->flags & S_NOATIME)
1292                         flags |= EXT3_NOATIME_FL;
1293
1294                 ptlrpc_req_finished (req);
1295
1296                 RETURN(put_user(flags, (int *)arg));
1297         }
1298         case EXT3_IOC_SETFLAGS: {
1299                 struct mdc_op_data op_data;
1300                 struct iattr attr;
1301                 struct obdo *oa;
1302                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1303
1304                 if (get_user(flags, (int *)arg))
1305                         RETURN(-EFAULT);
1306
1307                 oa = obdo_alloc();
1308                 if (!oa)
1309                         RETURN(-ENOMEM);
1310
1311                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1312
1313                 memset(&attr, 0x0, sizeof(attr));
1314                 attr.ia_attr_flags = flags;
1315                 attr.ia_valid |= ATTR_ATTR_FLAG;
1316
1317                 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1318                                 &attr, NULL, 0, NULL, 0, &req);
1319                 if (rc) {
1320                         ptlrpc_req_finished(req);
1321                         if (rc != -EPERM && rc != -EACCES)
1322                                 CERROR("md_setattr fails: rc = %d\n", rc);
1323                         obdo_free(oa);
1324                         RETURN(rc);
1325                 }
1326                 ptlrpc_req_finished(req);
1327
1328                 oa->o_id = lsm->lsm_object_id;
1329                 oa->o_gr = lsm->lsm_object_gr;
1330                 oa->o_flags = flags;
1331                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1332
1333                 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1334                 obdo_free(oa);
1335                 if (rc) {
1336                         if (rc != -EPERM && rc != -EACCES)
1337                                 CERROR("md_setattr fails: rc = %d\n", rc);
1338                         RETURN(rc);
1339                 }
1340
1341                 if (flags & EXT3_APPEND_FL)
1342                         inode->i_flags |= S_APPEND;
1343                 else
1344                         inode->i_flags &= ~S_APPEND;
1345                 if (flags & EXT3_IMMUTABLE_FL)
1346                         inode->i_flags |= S_IMMUTABLE;
1347                 else
1348                         inode->i_flags &= ~S_IMMUTABLE;
1349                 if (flags & EXT3_NOATIME_FL)
1350                         inode->i_flags |= S_NOATIME;
1351                 else
1352                         inode->i_flags &= ~S_NOATIME;
1353
1354                 RETURN(0);
1355         }
1356         default:
1357                 RETURN(-ENOSYS);
1358         }
1359
1360         RETURN(0);
1361 }
1362
1363 void ll_umount_begin(struct super_block *sb)
1364 {
1365         struct ll_sb_info *sbi = ll_s2sbi(sb);
1366         struct obd_device *obd;
1367         struct obd_ioctl_data ioc_data = { 0 };
1368         ENTRY;
1369         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1370
1371         obd = class_exp2obd(sbi->ll_mdc_exp);
1372         if (obd == NULL) {
1373                 CERROR("Invalid MDC connection handle "LPX64"\n",
1374                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1375                 EXIT;
1376                 return;
1377         }
1378         obd->obd_no_recov = 1;
1379         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1380                       &ioc_data, NULL);
1381
1382         obd = class_exp2obd(sbi->ll_osc_exp);
1383         if (obd == NULL) {
1384                 CERROR("Invalid LOV connection handle "LPX64"\n",
1385                        sbi->ll_osc_exp->exp_handle.h_cookie);
1386                 EXIT;
1387                 return;
1388         }
1389
1390         obd->obd_no_recov = 1;
1391         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1392                       &ioc_data, NULL);
1393
1394         /* Really, we'd like to wait until there are no requests outstanding,
1395          * and then continue.  For now, we just invalidate the requests,
1396          * schedule, and hope.
1397          */
1398         schedule();
1399
1400         EXIT;
1401 }
1402
1403 int ll_prep_inode(struct obd_export *osc_exp, struct obd_export *mdc_exp,
1404                   struct inode **inode, struct ptlrpc_request *req,
1405                   int offset, struct super_block *sb)
1406 {
1407         struct lustre_md md;
1408         int rc = 0;
1409
1410         rc = mdc_req2lustre_md(mdc_exp, req, offset, osc_exp, &md);
1411         if (rc)
1412                 RETURN(rc);
1413
1414         if (*inode) {
1415                 ll_update_inode(*inode, &md);
1416         } else {
1417                 LASSERT(sb);
1418                 *inode = ll_iget(sb, md.body->ino, &md);
1419                 if (*inode == NULL || is_bad_inode(*inode)) {
1420                         /* free the lsm if we allocated one above */
1421                         if (md.lsm != NULL)
1422                                 obd_free_memmd(osc_exp, &md.lsm);
1423                         if (md.mea != NULL)
1424                                 obd_free_memmd(mdc_exp,
1425                                                (struct lov_stripe_md**)&md.mea);
1426                         rc = -ENOMEM;
1427                         CERROR("new_inode -fatal: rc %d\n", rc);
1428                 }
1429         }
1430
1431         RETURN(rc);
1432 }