1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/linux/linux-obdo.c
38 * Object Devices Class Driver
39 * These are the only exported functions, they provide some generic
40 * infrastructure for managing object devices
43 #define DEBUG_SUBSYSTEM S_CLASS
45 # define EXPORT_SYMTAB
49 #include <liblustre.h>
51 #include <linux/module.h>
52 #include <obd_class.h>
53 #include <lustre/lustre_idl.h>
58 #include <linux/pagemap.h> /* for PAGE_CACHE_SIZE */
60 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
62 if (ia_valid & ATTR_ATIME) {
63 oa->o_atime = LTIME_S(attr->ia_atime);
64 oa->o_valid |= OBD_MD_FLATIME;
66 if (ia_valid & ATTR_MTIME) {
67 oa->o_mtime = LTIME_S(attr->ia_mtime);
68 oa->o_valid |= OBD_MD_FLMTIME;
70 if (ia_valid & ATTR_CTIME) {
71 oa->o_ctime = LTIME_S(attr->ia_ctime);
72 oa->o_valid |= OBD_MD_FLCTIME;
74 if (ia_valid & ATTR_SIZE) {
75 oa->o_size = attr->ia_size;
76 oa->o_valid |= OBD_MD_FLSIZE;
78 if (ia_valid & ATTR_MODE) {
79 oa->o_mode = attr->ia_mode;
80 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
81 if (!in_group_p(oa->o_gid) && !cfs_capable(CFS_CAP_FSETID))
82 oa->o_mode &= ~S_ISGID;
84 if (ia_valid & ATTR_UID) {
85 oa->o_uid = attr->ia_uid;
86 oa->o_valid |= OBD_MD_FLUID;
88 if (ia_valid & ATTR_GID) {
89 oa->o_gid = attr->ia_gid;
90 oa->o_valid |= OBD_MD_FLGID;
93 EXPORT_SYMBOL(obdo_from_iattr);
95 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
99 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
100 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
101 oa->o_valid, oa->o_mtime, oa->o_ctime);
104 if (valid & OBD_MD_FLATIME) {
105 LTIME_S(attr->ia_atime) = oa->o_atime;
106 attr->ia_valid |= ATTR_ATIME;
108 if (valid & OBD_MD_FLMTIME) {
109 LTIME_S(attr->ia_mtime) = oa->o_mtime;
110 attr->ia_valid |= ATTR_MTIME;
112 if (valid & OBD_MD_FLCTIME) {
113 LTIME_S(attr->ia_ctime) = oa->o_ctime;
114 attr->ia_valid |= ATTR_CTIME;
116 if (valid & OBD_MD_FLSIZE) {
117 attr->ia_size = oa->o_size;
118 attr->ia_valid |= ATTR_SIZE;
120 #if 0 /* you shouldn't be able to change a file's type with setattr */
121 if (valid & OBD_MD_FLTYPE) {
122 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
123 attr->ia_valid |= ATTR_MODE;
126 if (valid & OBD_MD_FLMODE) {
127 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
128 attr->ia_valid |= ATTR_MODE;
129 if (!in_group_p(oa->o_gid) && !cfs_capable(CFS_CAP_FSETID))
130 attr->ia_mode &= ~S_ISGID;
132 if (valid & OBD_MD_FLUID) {
133 attr->ia_uid = oa->o_uid;
134 attr->ia_valid |= ATTR_UID;
136 if (valid & OBD_MD_FLGID) {
137 attr->ia_gid = oa->o_gid;
138 attr->ia_valid |= ATTR_GID;
141 EXPORT_SYMBOL(iattr_from_obdo);
143 /* WARNING: the file systems must take care not to tinker with
144 attributes they don't manage (such as blocks). */
145 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
147 obd_flag newvalid = 0;
149 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
150 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
151 valid, LTIME_S(src->i_mtime),
152 LTIME_S(src->i_ctime));
154 if (valid & OBD_MD_FLATIME) {
155 dst->o_atime = LTIME_S(src->i_atime);
156 newvalid |= OBD_MD_FLATIME;
158 if (valid & OBD_MD_FLMTIME) {
159 dst->o_mtime = LTIME_S(src->i_mtime);
160 newvalid |= OBD_MD_FLMTIME;
162 if (valid & OBD_MD_FLCTIME) {
163 dst->o_ctime = LTIME_S(src->i_ctime);
164 newvalid |= OBD_MD_FLCTIME;
166 if (valid & OBD_MD_FLSIZE) {
167 dst->o_size = i_size_read(src);
168 newvalid |= OBD_MD_FLSIZE;
170 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
171 dst->o_blocks = src->i_blocks;
172 newvalid |= OBD_MD_FLBLOCKS;
174 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
175 dst->o_blksize = 1<<src->i_blkbits;
176 newvalid |= OBD_MD_FLBLKSZ;
178 if (valid & OBD_MD_FLTYPE) {
179 dst->o_mode = (dst->o_mode & S_IALLUGO)|(src->i_mode & S_IFMT);
180 newvalid |= OBD_MD_FLTYPE;
182 if (valid & OBD_MD_FLMODE) {
183 dst->o_mode = (dst->o_mode & S_IFMT)|(src->i_mode & S_IALLUGO);
184 newvalid |= OBD_MD_FLMODE;
186 if (valid & OBD_MD_FLUID) {
187 dst->o_uid = src->i_uid;
188 newvalid |= OBD_MD_FLUID;
190 if (valid & OBD_MD_FLGID) {
191 dst->o_gid = src->i_gid;
192 newvalid |= OBD_MD_FLGID;
194 if (valid & OBD_MD_FLFLAGS) {
195 dst->o_flags = src->i_flags;
196 newvalid |= OBD_MD_FLFLAGS;
198 if (valid & OBD_MD_FLGENER) {
199 dst->o_generation = src->i_generation;
200 newvalid |= OBD_MD_FLGENER;
202 if (valid & OBD_MD_FLFID) {
203 dst->o_fid = src->i_ino;
204 newvalid |= OBD_MD_FLFID;
207 dst->o_valid |= newvalid;
209 EXPORT_SYMBOL(obdo_from_inode);
211 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
213 valid &= src->o_valid;
215 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
217 "valid "LPX64", cur time %lu/%lu, new "LPU64"/"LPU64"\n",
218 src->o_valid, LTIME_S(dst->i_mtime),
219 LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
221 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
222 LTIME_S(dst->i_atime) = src->o_atime;
223 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
224 LTIME_S(dst->i_mtime) = src->o_mtime;
225 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
226 LTIME_S(dst->i_ctime) = src->o_ctime;
227 if (valid & OBD_MD_FLSIZE)
228 i_size_write(dst, src->o_size);
229 /* optimum IO size */
230 if (valid & OBD_MD_FLBLKSZ && src->o_blksize > (1<<dst->i_blkbits)) {
231 dst->i_blkbits = ffs(src->o_blksize)-1;
232 #ifdef HAVE_INODE_BLKSIZE
233 dst->i_blksize = src->o_blksize;
237 if (dst->i_blkbits < CFS_PAGE_SHIFT) {
238 #ifdef HAVE_INODE_BLKSIZE
239 dst->i_blksize = CFS_PAGE_SIZE;
241 dst->i_blkbits = CFS_PAGE_SHIFT;
244 /* allocation of space */
245 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
246 dst->i_blocks = src->o_blocks;
248 EXPORT_SYMBOL(obdo_refresh_inode);
250 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
252 valid &= src->o_valid;
254 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
256 "valid "LPX64", cur time %lu/%lu, new "LPU64"/"LPU64"\n",
257 src->o_valid, LTIME_S(dst->i_mtime),
258 LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
260 if (valid & OBD_MD_FLATIME)
261 LTIME_S(dst->i_atime) = src->o_atime;
262 if (valid & OBD_MD_FLMTIME)
263 LTIME_S(dst->i_mtime) = src->o_mtime;
264 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
265 LTIME_S(dst->i_ctime) = src->o_ctime;
266 if (valid & OBD_MD_FLSIZE)
267 i_size_write(dst, src->o_size);
268 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space */
269 dst->i_blocks = src->o_blocks;
270 if (dst->i_blocks < src->o_blocks) /* overflow */
274 if (valid & OBD_MD_FLBLKSZ) {
275 dst->i_blkbits = ffs(src->o_blksize)-1;
276 #ifdef HAVE_INODE_BLKSIZE
277 dst->i_blksize = src->o_blksize;
280 if (valid & OBD_MD_FLTYPE)
281 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
282 if (valid & OBD_MD_FLMODE)
283 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
284 if (valid & OBD_MD_FLUID)
285 dst->i_uid = src->o_uid;
286 if (valid & OBD_MD_FLGID)
287 dst->i_gid = src->o_gid;
288 if (valid & OBD_MD_FLFLAGS)
289 dst->i_flags = src->o_flags;
290 if (valid & OBD_MD_FLGENER)
291 dst->i_generation = src->o_generation;
293 EXPORT_SYMBOL(obdo_to_inode);