blob: 79104a66da96b68da15fc1e9474871eeed965852 [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright (c) 2011, 2012, Intel Corporation.
27 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 *
32 * lustre/obdclass/obdo.c
33 *
34 * Object Devices Class Driver
35 * These are the only exported functions, they provide some generic
36 * infrastructure for managing object devices
37 */
38
39#define DEBUG_SUBSYSTEM S_CLASS
40
Greg Kroah-Hartman610f7372014-07-11 22:15:24 -070041#include "../include/obd_class.h"
42#include "../include/lustre/lustre_idl.h"
Peng Taod7e09d02013-05-02 16:46:55 +080043
44void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
45{
46 dst->o_parent_oid = fid_oid(parent);
47 dst->o_parent_seq = fid_seq(parent);
48 dst->o_parent_ver = fid_ver(parent);
49 dst->o_valid |= OBD_MD_FLGENER | OBD_MD_FLFID;
50}
51EXPORT_SYMBOL(obdo_set_parent_fid);
52
53/* WARNING: the file systems must take care not to tinker with
Oleg Drokin6ba59172016-02-24 22:00:35 -050054 * attributes they don't manage (such as blocks).
55 */
Oleg Drokin21aef7d2014-08-15 12:55:56 -040056void obdo_from_inode(struct obdo *dst, struct inode *src, u32 valid)
Peng Taod7e09d02013-05-02 16:46:55 +080057{
Oleg Drokin21aef7d2014-08-15 12:55:56 -040058 u32 newvalid = 0;
Peng Taod7e09d02013-05-02 16:46:55 +080059
60 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
61 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
62 valid, LTIME_S(src->i_mtime),
63 LTIME_S(src->i_ctime));
64
65 if (valid & OBD_MD_FLATIME) {
66 dst->o_atime = LTIME_S(src->i_atime);
67 newvalid |= OBD_MD_FLATIME;
68 }
69 if (valid & OBD_MD_FLMTIME) {
70 dst->o_mtime = LTIME_S(src->i_mtime);
71 newvalid |= OBD_MD_FLMTIME;
72 }
73 if (valid & OBD_MD_FLCTIME) {
74 dst->o_ctime = LTIME_S(src->i_ctime);
75 newvalid |= OBD_MD_FLCTIME;
76 }
77 if (valid & OBD_MD_FLSIZE) {
78 dst->o_size = i_size_read(src);
79 newvalid |= OBD_MD_FLSIZE;
80 }
81 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
82 dst->o_blocks = src->i_blocks;
83 newvalid |= OBD_MD_FLBLOCKS;
84 }
85 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
John L. Hammond16e06312014-09-09 13:39:03 -050086 dst->o_blksize = 1 << src->i_blkbits;
Peng Taod7e09d02013-05-02 16:46:55 +080087 newvalid |= OBD_MD_FLBLKSZ;
88 }
89 if (valid & OBD_MD_FLTYPE) {
90 dst->o_mode = (dst->o_mode & S_IALLUGO) |
91 (src->i_mode & S_IFMT);
92 newvalid |= OBD_MD_FLTYPE;
93 }
94 if (valid & OBD_MD_FLMODE) {
95 dst->o_mode = (dst->o_mode & S_IFMT) |
96 (src->i_mode & S_IALLUGO);
97 newvalid |= OBD_MD_FLMODE;
98 }
99 if (valid & OBD_MD_FLUID) {
Peng Tao4b1a25f2013-07-15 22:27:14 +0800100 dst->o_uid = from_kuid(&init_user_ns, src->i_uid);
Peng Taod7e09d02013-05-02 16:46:55 +0800101 newvalid |= OBD_MD_FLUID;
102 }
103 if (valid & OBD_MD_FLGID) {
Peng Tao4b1a25f2013-07-15 22:27:14 +0800104 dst->o_gid = from_kgid(&init_user_ns, src->i_gid);
Peng Taod7e09d02013-05-02 16:46:55 +0800105 newvalid |= OBD_MD_FLGID;
106 }
107 if (valid & OBD_MD_FLFLAGS) {
John L. Hammond616331c2014-08-28 18:35:18 -0500108 dst->o_flags = src->i_flags;
Peng Taod7e09d02013-05-02 16:46:55 +0800109 newvalid |= OBD_MD_FLFLAGS;
110 }
111 dst->o_valid |= newvalid;
112}
113EXPORT_SYMBOL(obdo_from_inode);
114
John L. Hammond58c78cd2016-08-19 14:07:29 -0400115void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj)
Peng Taod7e09d02013-05-02 16:46:55 +0800116{
117 ioobj->ioo_oid = oa->o_oi;
118 if (unlikely(!(oa->o_valid & OBD_MD_FLGROUP)))
119 ostid_set_seq_mdt0(&ioobj->ioo_oid);
120
121 /* Since 2.4 this does not contain o_mode in the low 16 bits.
Oleg Drokin6ba59172016-02-24 22:00:35 -0500122 * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs
123 */
Peng Taod7e09d02013-05-02 16:46:55 +0800124 ioobj->ioo_max_brw = 0;
125}
126EXPORT_SYMBOL(obdo_to_ioobj);
127
John L. Hammond58c78cd2016-08-19 14:07:29 -0400128static void iattr_from_obdo(struct iattr *attr, const struct obdo *oa,
129 u32 valid)
Peng Taod7e09d02013-05-02 16:46:55 +0800130{
131 valid &= oa->o_valid;
132
133 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700134 CDEBUG(D_INODE, "valid %#llx, new time %llu/%llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800135 oa->o_valid, oa->o_mtime, oa->o_ctime);
136
137 attr->ia_valid = 0;
138 if (valid & OBD_MD_FLATIME) {
139 LTIME_S(attr->ia_atime) = oa->o_atime;
140 attr->ia_valid |= ATTR_ATIME;
141 }
142 if (valid & OBD_MD_FLMTIME) {
143 LTIME_S(attr->ia_mtime) = oa->o_mtime;
144 attr->ia_valid |= ATTR_MTIME;
145 }
146 if (valid & OBD_MD_FLCTIME) {
147 LTIME_S(attr->ia_ctime) = oa->o_ctime;
148 attr->ia_valid |= ATTR_CTIME;
149 }
150 if (valid & OBD_MD_FLSIZE) {
151 attr->ia_size = oa->o_size;
152 attr->ia_valid |= ATTR_SIZE;
153 }
154#if 0 /* you shouldn't be able to change a file's type with setattr */
155 if (valid & OBD_MD_FLTYPE) {
Oleg Drokincd94f232016-08-21 18:04:34 -0400156 attr->ia_mode = (attr->ia_mode & ~S_IFMT) |
157 (oa->o_mode & S_IFMT);
Peng Taod7e09d02013-05-02 16:46:55 +0800158 attr->ia_valid |= ATTR_MODE;
159 }
160#endif
161 if (valid & OBD_MD_FLMODE) {
Oleg Drokincd94f232016-08-21 18:04:34 -0400162 attr->ia_mode = (attr->ia_mode & S_IFMT) |
163 (oa->o_mode & ~S_IFMT);
Peng Taod7e09d02013-05-02 16:46:55 +0800164 attr->ia_valid |= ATTR_MODE;
Peng Tao4b1a25f2013-07-15 22:27:14 +0800165 if (!in_group_p(make_kgid(&init_user_ns, oa->o_gid)) &&
Peng Tao2eb90a72014-01-22 21:47:37 +0800166 !capable(CFS_CAP_FSETID))
Peng Taod7e09d02013-05-02 16:46:55 +0800167 attr->ia_mode &= ~S_ISGID;
168 }
169 if (valid & OBD_MD_FLUID) {
Peng Tao4b1a25f2013-07-15 22:27:14 +0800170 attr->ia_uid = make_kuid(&init_user_ns, oa->o_uid);
Peng Taod7e09d02013-05-02 16:46:55 +0800171 attr->ia_valid |= ATTR_UID;
172 }
173 if (valid & OBD_MD_FLGID) {
Peng Tao4b1a25f2013-07-15 22:27:14 +0800174 attr->ia_gid = make_kgid(&init_user_ns, oa->o_gid);
Peng Taod7e09d02013-05-02 16:46:55 +0800175 attr->ia_valid |= ATTR_GID;
176 }
177}
Peng Taod7e09d02013-05-02 16:46:55 +0800178
John L. Hammond58c78cd2016-08-19 14:07:29 -0400179void md_from_obdo(struct md_op_data *op_data, const struct obdo *oa, u32 valid)
Peng Taod7e09d02013-05-02 16:46:55 +0800180{
181 iattr_from_obdo(&op_data->op_attr, oa, valid);
182 if (valid & OBD_MD_FLBLOCKS) {
183 op_data->op_attr_blocks = oa->o_blocks;
184 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
185 }
186 if (valid & OBD_MD_FLFLAGS) {
Oleg Drokinbb412922016-03-30 19:48:38 -0400187 op_data->op_attr_flags = oa->o_flags;
Peng Taod7e09d02013-05-02 16:46:55 +0800188 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
189 }
190}
191EXPORT_SYMBOL(md_from_obdo);