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