Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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 |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * 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-Hartman | 610f737 | 2014-07-11 22:15:24 -0700 | [diff] [blame] | 41 | #include "../include/obd_class.h" |
| 42 | #include "../include/lustre/lustre_idl.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 43 | |
| 44 | void 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 | } |
| 51 | EXPORT_SYMBOL(obdo_set_parent_fid); |
| 52 | |
| 53 | /* WARNING: the file systems must take care not to tinker with |
Oleg Drokin | 6ba5917 | 2016-02-24 22:00:35 -0500 | [diff] [blame] | 54 | * attributes they don't manage (such as blocks). |
| 55 | */ |
Oleg Drokin | 21aef7d | 2014-08-15 12:55:56 -0400 | [diff] [blame] | 56 | void obdo_from_inode(struct obdo *dst, struct inode *src, u32 valid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 57 | { |
Oleg Drokin | 21aef7d | 2014-08-15 12:55:56 -0400 | [diff] [blame] | 58 | u32 newvalid = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 59 | |
| 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. Hammond | 16e0631 | 2014-09-09 13:39:03 -0500 | [diff] [blame] | 86 | dst->o_blksize = 1 << src->i_blkbits; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 87 | 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 Tao | 4b1a25f | 2013-07-15 22:27:14 +0800 | [diff] [blame] | 100 | dst->o_uid = from_kuid(&init_user_ns, src->i_uid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 101 | newvalid |= OBD_MD_FLUID; |
| 102 | } |
| 103 | if (valid & OBD_MD_FLGID) { |
Peng Tao | 4b1a25f | 2013-07-15 22:27:14 +0800 | [diff] [blame] | 104 | dst->o_gid = from_kgid(&init_user_ns, src->i_gid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 105 | newvalid |= OBD_MD_FLGID; |
| 106 | } |
| 107 | if (valid & OBD_MD_FLFLAGS) { |
John L. Hammond | 616331c | 2014-08-28 18:35:18 -0500 | [diff] [blame] | 108 | dst->o_flags = src->i_flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 109 | newvalid |= OBD_MD_FLFLAGS; |
| 110 | } |
| 111 | dst->o_valid |= newvalid; |
| 112 | } |
| 113 | EXPORT_SYMBOL(obdo_from_inode); |
| 114 | |
John L. Hammond | 58c78cd | 2016-08-19 14:07:29 -0400 | [diff] [blame] | 115 | void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 116 | { |
| 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 Drokin | 6ba5917 | 2016-02-24 22:00:35 -0500 | [diff] [blame] | 122 | * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs |
| 123 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 124 | ioobj->ioo_max_brw = 0; |
| 125 | } |
| 126 | EXPORT_SYMBOL(obdo_to_ioobj); |
| 127 | |
John L. Hammond | 58c78cd | 2016-08-19 14:07:29 -0400 | [diff] [blame] | 128 | static void iattr_from_obdo(struct iattr *attr, const struct obdo *oa, |
| 129 | u32 valid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 130 | { |
| 131 | valid &= oa->o_valid; |
| 132 | |
| 133 | if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME)) |
Greg Kroah-Hartman | 55f5a82 | 2014-07-12 20:26:07 -0700 | [diff] [blame] | 134 | CDEBUG(D_INODE, "valid %#llx, new time %llu/%llu\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 135 | 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 Drokin | cd94f23 | 2016-08-21 18:04:34 -0400 | [diff] [blame] | 156 | attr->ia_mode = (attr->ia_mode & ~S_IFMT) | |
| 157 | (oa->o_mode & S_IFMT); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 158 | attr->ia_valid |= ATTR_MODE; |
| 159 | } |
| 160 | #endif |
| 161 | if (valid & OBD_MD_FLMODE) { |
Oleg Drokin | cd94f23 | 2016-08-21 18:04:34 -0400 | [diff] [blame] | 162 | attr->ia_mode = (attr->ia_mode & S_IFMT) | |
| 163 | (oa->o_mode & ~S_IFMT); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 164 | attr->ia_valid |= ATTR_MODE; |
Peng Tao | 4b1a25f | 2013-07-15 22:27:14 +0800 | [diff] [blame] | 165 | if (!in_group_p(make_kgid(&init_user_ns, oa->o_gid)) && |
Peng Tao | 2eb90a7 | 2014-01-22 21:47:37 +0800 | [diff] [blame] | 166 | !capable(CFS_CAP_FSETID)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 167 | attr->ia_mode &= ~S_ISGID; |
| 168 | } |
| 169 | if (valid & OBD_MD_FLUID) { |
Peng Tao | 4b1a25f | 2013-07-15 22:27:14 +0800 | [diff] [blame] | 170 | attr->ia_uid = make_kuid(&init_user_ns, oa->o_uid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 171 | attr->ia_valid |= ATTR_UID; |
| 172 | } |
| 173 | if (valid & OBD_MD_FLGID) { |
Peng Tao | 4b1a25f | 2013-07-15 22:27:14 +0800 | [diff] [blame] | 174 | attr->ia_gid = make_kgid(&init_user_ns, oa->o_gid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 175 | attr->ia_valid |= ATTR_GID; |
| 176 | } |
| 177 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 178 | |
John L. Hammond | 58c78cd | 2016-08-19 14:07:29 -0400 | [diff] [blame] | 179 | void md_from_obdo(struct md_op_data *op_data, const struct obdo *oa, u32 valid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 180 | { |
| 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 Drokin | bb41292 | 2016-03-30 19:48:38 -0400 | [diff] [blame] | 187 | op_data->op_attr_flags = oa->o_flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 188 | op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG; |
| 189 | } |
| 190 | } |
| 191 | EXPORT_SYMBOL(md_from_obdo); |