[go: nahoru, domu]

1/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26/*
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2011, 2012, Intel Corporation.
31 */
32/*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * lustre/obdclass/linux/linux-obdo.c
37 *
38 * Object Devices Class Driver
39 * These are the only exported functions, they provide some generic
40 * infrastructure for managing object devices
41 */
42
43#define DEBUG_SUBSYSTEM S_CLASS
44
45#include <linux/module.h>
46#include "../../include/obd_class.h"
47#include "../../include/lustre/lustre_idl.h"
48
49#include <linux/fs.h>
50#include <linux/pagemap.h> /* for PAGE_CACHE_SIZE */
51
52/*FIXME: Just copy from obdo_from_inode*/
53void obdo_from_la(struct obdo *dst, struct lu_attr *la, __u64 valid)
54{
55	u32 newvalid = 0;
56
57	if (valid & LA_ATIME) {
58		dst->o_atime = la->la_atime;
59		newvalid |= OBD_MD_FLATIME;
60	}
61	if (valid & LA_MTIME) {
62		dst->o_mtime = la->la_mtime;
63		newvalid |= OBD_MD_FLMTIME;
64	}
65	if (valid & LA_CTIME) {
66		dst->o_ctime = la->la_ctime;
67		newvalid |= OBD_MD_FLCTIME;
68	}
69	if (valid & LA_SIZE) {
70		dst->o_size = la->la_size;
71		newvalid |= OBD_MD_FLSIZE;
72	}
73	if (valid & LA_BLOCKS) {  /* allocation of space (x512 bytes) */
74		dst->o_blocks = la->la_blocks;
75		newvalid |= OBD_MD_FLBLOCKS;
76	}
77	if (valid & LA_TYPE) {
78		dst->o_mode = (dst->o_mode & S_IALLUGO) |
79			      (la->la_mode & S_IFMT);
80		newvalid |= OBD_MD_FLTYPE;
81	}
82	if (valid & LA_MODE) {
83		dst->o_mode = (dst->o_mode & S_IFMT) |
84			      (la->la_mode & S_IALLUGO);
85		newvalid |= OBD_MD_FLMODE;
86	}
87	if (valid & LA_UID) {
88		dst->o_uid = la->la_uid;
89		newvalid |= OBD_MD_FLUID;
90	}
91	if (valid & LA_GID) {
92		dst->o_gid = la->la_gid;
93		newvalid |= OBD_MD_FLGID;
94	}
95	dst->o_valid |= newvalid;
96}
97EXPORT_SYMBOL(obdo_from_la);
98
99/*FIXME: Just copy from obdo_from_inode*/
100void la_from_obdo(struct lu_attr *dst, struct obdo *obdo, u32 valid)
101{
102	__u64 newvalid = 0;
103
104	valid &= obdo->o_valid;
105
106	if (valid & OBD_MD_FLATIME) {
107		dst->la_atime = obdo->o_atime;
108		newvalid |= LA_ATIME;
109	}
110	if (valid & OBD_MD_FLMTIME) {
111		dst->la_mtime = obdo->o_mtime;
112		newvalid |= LA_MTIME;
113	}
114	if (valid & OBD_MD_FLCTIME) {
115		dst->la_ctime = obdo->o_ctime;
116		newvalid |= LA_CTIME;
117	}
118	if (valid & OBD_MD_FLSIZE) {
119		dst->la_size = obdo->o_size;
120		newvalid |= LA_SIZE;
121	}
122	if (valid & OBD_MD_FLBLOCKS) {
123		dst->la_blocks = obdo->o_blocks;
124		newvalid |= LA_BLOCKS;
125	}
126	if (valid & OBD_MD_FLTYPE) {
127		dst->la_mode = (dst->la_mode & S_IALLUGO) |
128			       (obdo->o_mode & S_IFMT);
129		newvalid |= LA_TYPE;
130	}
131	if (valid & OBD_MD_FLMODE) {
132		dst->la_mode = (dst->la_mode & S_IFMT) |
133			       (obdo->o_mode & S_IALLUGO);
134		newvalid |= LA_MODE;
135	}
136	if (valid & OBD_MD_FLUID) {
137		dst->la_uid = obdo->o_uid;
138		newvalid |= LA_UID;
139	}
140	if (valid & OBD_MD_FLGID) {
141		dst->la_gid = obdo->o_gid;
142		newvalid |= LA_GID;
143	}
144	dst->la_valid = newvalid;
145}
146EXPORT_SYMBOL(la_from_obdo);
147
148void obdo_refresh_inode(struct inode *dst, struct obdo *src, u32 valid)
149{
150	valid &= src->o_valid;
151
152	if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
153		CDEBUG(D_INODE,
154		       "valid %#llx, cur time %lu/%lu, new %llu/%llu\n",
155		       src->o_valid, LTIME_S(dst->i_mtime),
156		       LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
157
158	if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
159		LTIME_S(dst->i_atime) = src->o_atime;
160	if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
161		LTIME_S(dst->i_mtime) = src->o_mtime;
162	if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
163		LTIME_S(dst->i_ctime) = src->o_ctime;
164	if (valid & OBD_MD_FLSIZE)
165		i_size_write(dst, src->o_size);
166	/* optimum IO size */
167	if (valid & OBD_MD_FLBLKSZ && src->o_blksize > (1 << dst->i_blkbits))
168		dst->i_blkbits = ffs(src->o_blksize) - 1;
169
170	if (dst->i_blkbits < PAGE_CACHE_SHIFT)
171		dst->i_blkbits = PAGE_CACHE_SHIFT;
172
173	/* allocation of space */
174	if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
175		/*
176		 * XXX shouldn't overflow be checked here like in
177		 * obdo_to_inode().
178		 */
179		dst->i_blocks = src->o_blocks;
180}
181EXPORT_SYMBOL(obdo_refresh_inode);
182
183void obdo_to_inode(struct inode *dst, struct obdo *src, u32 valid)
184{
185	valid &= src->o_valid;
186
187	LASSERTF(!(valid & (OBD_MD_FLTYPE | OBD_MD_FLGENER | OBD_MD_FLFID |
188			    OBD_MD_FLID | OBD_MD_FLGROUP)),
189		 "object "DOSTID", valid %x\n", POSTID(&src->o_oi), valid);
190
191	if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
192		CDEBUG(D_INODE,
193		       "valid %#llx, cur time %lu/%lu, new %llu/%llu\n",
194		       src->o_valid, LTIME_S(dst->i_mtime),
195		       LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
196
197	if (valid & OBD_MD_FLATIME)
198		LTIME_S(dst->i_atime) = src->o_atime;
199	if (valid & OBD_MD_FLMTIME)
200		LTIME_S(dst->i_mtime) = src->o_mtime;
201	if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
202		LTIME_S(dst->i_ctime) = src->o_ctime;
203	if (valid & OBD_MD_FLSIZE)
204		i_size_write(dst, src->o_size);
205	if (valid & OBD_MD_FLBLOCKS) { /* allocation of space */
206		dst->i_blocks = src->o_blocks;
207		if (dst->i_blocks < src->o_blocks) /* overflow */
208			dst->i_blocks = -1;
209
210	}
211	if (valid & OBD_MD_FLBLKSZ)
212		dst->i_blkbits = ffs(src->o_blksize)-1;
213	if (valid & OBD_MD_FLMODE)
214		dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
215	if (valid & OBD_MD_FLUID)
216		dst->i_uid = make_kuid(&init_user_ns, src->o_uid);
217	if (valid & OBD_MD_FLGID)
218		dst->i_gid = make_kgid(&init_user_ns, src->o_gid);
219	if (valid & OBD_MD_FLFLAGS)
220		dst->i_flags = src->o_flags;
221}
222EXPORT_SYMBOL(obdo_to_inode);
223