Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) International Business Machines Corp., 2000-2004 |
| 3 | * Copyright (C) Christoph Hellwig, 2002 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 7 | * the Free Software Foundation; either version 2 of the License, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * (at your option) any later version. |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 16 | * along with this program; if not, write to the Free Software |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 20 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/fs.h> |
| 22 | #include <linux/xattr.h> |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 23 | #include <linux/posix_acl_xattr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/quotaops.h> |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 26 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "jfs_incore.h" |
| 28 | #include "jfs_superblock.h" |
| 29 | #include "jfs_dmap.h" |
| 30 | #include "jfs_debug.h" |
| 31 | #include "jfs_dinode.h" |
| 32 | #include "jfs_extent.h" |
| 33 | #include "jfs_metapage.h" |
| 34 | #include "jfs_xattr.h" |
| 35 | #include "jfs_acl.h" |
| 36 | |
| 37 | /* |
| 38 | * jfs_xattr.c: extended attribute service |
| 39 | * |
| 40 | * Overall design -- |
| 41 | * |
| 42 | * Format: |
| 43 | * |
| 44 | * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit |
| 45 | * value) and a variable (0 or more) number of extended attribute |
| 46 | * entries. Each extended attribute entry (jfs_ea) is a <name,value> double |
| 47 | * where <name> is constructed from a null-terminated ascii string |
| 48 | * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data |
| 49 | * (1 ... 65535 bytes). The in-memory format is |
| 50 | * |
| 51 | * 0 1 2 4 4 + namelen + 1 |
| 52 | * +-------+--------+--------+----------------+-------------------+ |
| 53 | * | Flags | Name | Value | Name String \0 | Data . . . . | |
| 54 | * | | Length | Length | | | |
| 55 | * +-------+--------+--------+----------------+-------------------+ |
| 56 | * |
| 57 | * A jfs_ea_list then is structured as |
| 58 | * |
| 59 | * 0 4 4 + EA_SIZE(ea1) |
| 60 | * +------------+-------------------+--------------------+----- |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 61 | * | Overall EA | First FEA Element | Second FEA Element | ..... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * | List Size | | | |
| 63 | * +------------+-------------------+--------------------+----- |
| 64 | * |
| 65 | * On-disk: |
| 66 | * |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 67 | * FEALISTs are stored on disk using blocks allocated by dbAlloc() and |
| 68 | * written directly. An EA list may be in-lined in the inode if there is |
| 69 | * sufficient room available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | */ |
| 71 | |
| 72 | struct ea_buffer { |
| 73 | int flag; /* Indicates what storage xattr points to */ |
| 74 | int max_size; /* largest xattr that fits in current buffer */ |
| 75 | dxd_t new_ea; /* dxd to replace ea when modifying xattr */ |
| 76 | struct metapage *mp; /* metapage containing ea list */ |
| 77 | struct jfs_ea_list *xattr; /* buffer containing ea list */ |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * ea_buffer.flag values |
| 82 | */ |
| 83 | #define EA_INLINE 0x0001 |
| 84 | #define EA_EXTENT 0x0002 |
| 85 | #define EA_NEW 0x0004 |
| 86 | #define EA_MALLOC 0x0008 |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Dave Kleikamp | aca0fa3 | 2010-08-09 15:57:38 -0500 | [diff] [blame] | 89 | static int is_known_namespace(const char *name) |
| 90 | { |
| 91 | if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) && |
| 92 | strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
| 93 | strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) && |
| 94 | strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) |
| 95 | return false; |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* |
| 101 | * These three routines are used to recognize on-disk extended attributes |
| 102 | * that are in a recognized namespace. If the attribute is not recognized, |
| 103 | * "os2." is prepended to the name |
| 104 | */ |
Dave Kleikamp | aca0fa3 | 2010-08-09 15:57:38 -0500 | [diff] [blame] | 105 | static int is_os2_xattr(struct jfs_ea *ea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Dave Kleikamp | aca0fa3 | 2010-08-09 15:57:38 -0500 | [diff] [blame] | 107 | return !is_known_namespace(ea->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static inline int name_size(struct jfs_ea *ea) |
| 111 | { |
| 112 | if (is_os2_xattr(ea)) |
| 113 | return ea->namelen + XATTR_OS2_PREFIX_LEN; |
| 114 | else |
| 115 | return ea->namelen; |
| 116 | } |
| 117 | |
| 118 | static inline int copy_name(char *buffer, struct jfs_ea *ea) |
| 119 | { |
| 120 | int len = ea->namelen; |
| 121 | |
| 122 | if (is_os2_xattr(ea)) { |
| 123 | memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN); |
| 124 | buffer += XATTR_OS2_PREFIX_LEN; |
| 125 | len += XATTR_OS2_PREFIX_LEN; |
| 126 | } |
| 127 | memcpy(buffer, ea->name, ea->namelen); |
| 128 | buffer[ea->namelen] = 0; |
| 129 | |
| 130 | return len; |
| 131 | } |
| 132 | |
| 133 | /* Forward references */ |
| 134 | static void ea_release(struct inode *inode, struct ea_buffer *ea_buf); |
| 135 | |
| 136 | /* |
| 137 | * NAME: ea_write_inline |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 138 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * FUNCTION: Attempt to write an EA inline if area is available |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 140 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * PRE CONDITIONS: |
| 142 | * Already verified that the specified EA is small enough to fit inline |
| 143 | * |
| 144 | * PARAMETERS: |
| 145 | * ip - Inode pointer |
| 146 | * ealist - EA list pointer |
| 147 | * size - size of ealist in bytes |
| 148 | * ea - dxd_t structure to be filled in with necessary EA information |
| 149 | * if we successfully copy the EA inline |
| 150 | * |
| 151 | * NOTES: |
| 152 | * Checks if the inode's inline area is available. If so, copies EA inline |
| 153 | * and sets <ea> fields appropriately. Otherwise, returns failure, EA will |
| 154 | * have to be put into an extent. |
| 155 | * |
| 156 | * RETURNS: 0 for successful copy to inline area; -1 if area not available |
| 157 | */ |
| 158 | static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist, |
| 159 | int size, dxd_t * ea) |
| 160 | { |
| 161 | struct jfs_inode_info *ji = JFS_IP(ip); |
| 162 | |
| 163 | /* |
| 164 | * Make sure we have an EA -- the NULL EA list is valid, but you |
| 165 | * can't copy it! |
| 166 | */ |
| 167 | if (ealist && size > sizeof (struct jfs_ea_list)) { |
| 168 | assert(size <= sizeof (ji->i_inline_ea)); |
| 169 | |
| 170 | /* |
| 171 | * See if the space is available or if it is already being |
| 172 | * used for an inline EA. |
| 173 | */ |
| 174 | if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE)) |
| 175 | return -EPERM; |
| 176 | |
| 177 | DXDsize(ea, size); |
| 178 | DXDlength(ea, 0); |
| 179 | DXDaddress(ea, 0); |
| 180 | memcpy(ji->i_inline_ea, ealist, size); |
| 181 | ea->flag = DXD_INLINE; |
| 182 | ji->mode2 &= ~INLINEEA; |
| 183 | } else { |
| 184 | ea->flag = 0; |
| 185 | DXDsize(ea, 0); |
| 186 | DXDlength(ea, 0); |
| 187 | DXDaddress(ea, 0); |
| 188 | |
| 189 | /* Free up INLINE area */ |
| 190 | if (ji->ea.flag & DXD_INLINE) |
| 191 | ji->mode2 |= INLINEEA; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * NAME: ea_write |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 199 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * FUNCTION: Write an EA for an inode |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 201 | * |
| 202 | * PRE CONDITIONS: EA has been verified |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * |
| 204 | * PARAMETERS: |
| 205 | * ip - Inode pointer |
| 206 | * ealist - EA list pointer |
| 207 | * size - size of ealist in bytes |
| 208 | * ea - dxd_t structure to be filled in appropriately with where the |
| 209 | * EA was copied |
| 210 | * |
| 211 | * NOTES: Will write EA inline if able to, otherwise allocates blocks for an |
| 212 | * extent and synchronously writes it to those blocks. |
| 213 | * |
| 214 | * RETURNS: 0 for success; Anything else indicates failure |
| 215 | */ |
| 216 | static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size, |
| 217 | dxd_t * ea) |
| 218 | { |
| 219 | struct super_block *sb = ip->i_sb; |
| 220 | struct jfs_inode_info *ji = JFS_IP(ip); |
| 221 | struct jfs_sb_info *sbi = JFS_SBI(sb); |
| 222 | int nblocks; |
| 223 | s64 blkno; |
| 224 | int rc = 0, i; |
| 225 | char *cp; |
| 226 | s32 nbytes, nb; |
| 227 | s32 bytes_to_write; |
| 228 | struct metapage *mp; |
| 229 | |
| 230 | /* |
| 231 | * Quick check to see if this is an in-linable EA. Short EAs |
| 232 | * and empty EAs are all in-linable, provided the space exists. |
| 233 | */ |
| 234 | if (!ealist || size <= sizeof (ji->i_inline_ea)) { |
| 235 | if (!ea_write_inline(ip, ealist, size, ea)) |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* figure out how many blocks we need */ |
| 240 | nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits; |
| 241 | |
| 242 | /* Allocate new blocks to quota. */ |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 243 | rc = dquot_alloc_block(ip, nblocks); |
| 244 | if (rc) |
| 245 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno); |
| 248 | if (rc) { |
| 249 | /*Rollback quota allocation. */ |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 250 | dquot_free_block(ip, nblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Now have nblocks worth of storage to stuff into the FEALIST. |
| 256 | * loop over the FEALIST copying data into the buffer one page at |
| 257 | * a time. |
| 258 | */ |
| 259 | cp = (char *) ealist; |
| 260 | nbytes = size; |
| 261 | for (i = 0; i < nblocks; i += sbi->nbperpage) { |
| 262 | /* |
| 263 | * Determine how many bytes for this request, and round up to |
| 264 | * the nearest aggregate block size |
| 265 | */ |
| 266 | nb = min(PSIZE, nbytes); |
| 267 | bytes_to_write = |
| 268 | ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits)) |
| 269 | << sb->s_blocksize_bits; |
| 270 | |
| 271 | if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) { |
| 272 | rc = -EIO; |
| 273 | goto failed; |
| 274 | } |
| 275 | |
| 276 | memcpy(mp->data, cp, nb); |
| 277 | |
| 278 | /* |
| 279 | * We really need a way to propagate errors for |
| 280 | * forced writes like this one. --hch |
| 281 | * |
| 282 | * (__write_metapage => release_metapage => flush_metapage) |
| 283 | */ |
| 284 | #ifdef _JFS_FIXME |
| 285 | if ((rc = flush_metapage(mp))) { |
| 286 | /* |
| 287 | * the write failed -- this means that the buffer |
| 288 | * is still assigned and the blocks are not being |
| 289 | * used. this seems like the best error recovery |
| 290 | * we can get ... |
| 291 | */ |
| 292 | goto failed; |
| 293 | } |
| 294 | #else |
| 295 | flush_metapage(mp); |
| 296 | #endif |
| 297 | |
| 298 | cp += PSIZE; |
| 299 | nbytes -= nb; |
| 300 | } |
| 301 | |
| 302 | ea->flag = DXD_EXTENT; |
| 303 | DXDsize(ea, le32_to_cpu(ealist->size)); |
| 304 | DXDlength(ea, nblocks); |
| 305 | DXDaddress(ea, blkno); |
| 306 | |
| 307 | /* Free up INLINE area */ |
| 308 | if (ji->ea.flag & DXD_INLINE) |
| 309 | ji->mode2 |= INLINEEA; |
| 310 | |
| 311 | return 0; |
| 312 | |
| 313 | failed: |
| 314 | /* Rollback quota allocation. */ |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 315 | dquot_free_block(ip, nblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | dbFree(ip, blkno, nblocks); |
| 318 | return rc; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * NAME: ea_read_inline |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 323 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | * FUNCTION: Read an inlined EA into user's buffer |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 325 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | * PARAMETERS: |
| 327 | * ip - Inode pointer |
| 328 | * ealist - Pointer to buffer to fill in with EA |
| 329 | * |
| 330 | * RETURNS: 0 |
| 331 | */ |
| 332 | static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist) |
| 333 | { |
| 334 | struct jfs_inode_info *ji = JFS_IP(ip); |
| 335 | int ea_size = sizeDXD(&ji->ea); |
| 336 | |
| 337 | if (ea_size == 0) { |
| 338 | ealist->size = 0; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* Sanity Check */ |
| 343 | if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea))) |
| 344 | return -EIO; |
| 345 | if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size) |
| 346 | != ea_size) |
| 347 | return -EIO; |
| 348 | |
| 349 | memcpy(ealist, ji->i_inline_ea, ea_size); |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * NAME: ea_read |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 355 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | * FUNCTION: copy EA data into user's buffer |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 357 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | * PARAMETERS: |
| 359 | * ip - Inode pointer |
| 360 | * ealist - Pointer to buffer to fill in with EA |
| 361 | * |
| 362 | * NOTES: If EA is inline calls ea_read_inline() to copy EA. |
| 363 | * |
| 364 | * RETURNS: 0 for success; other indicates failure |
| 365 | */ |
| 366 | static int ea_read(struct inode *ip, struct jfs_ea_list *ealist) |
| 367 | { |
| 368 | struct super_block *sb = ip->i_sb; |
| 369 | struct jfs_inode_info *ji = JFS_IP(ip); |
| 370 | struct jfs_sb_info *sbi = JFS_SBI(sb); |
| 371 | int nblocks; |
| 372 | s64 blkno; |
| 373 | char *cp = (char *) ealist; |
| 374 | int i; |
| 375 | int nbytes, nb; |
| 376 | s32 bytes_to_read; |
| 377 | struct metapage *mp; |
| 378 | |
| 379 | /* quick check for in-line EA */ |
| 380 | if (ji->ea.flag & DXD_INLINE) |
| 381 | return ea_read_inline(ip, ealist); |
| 382 | |
| 383 | nbytes = sizeDXD(&ji->ea); |
| 384 | if (!nbytes) { |
Joe Perches | eb8630d | 2013-06-04 16:39:15 -0700 | [diff] [blame] | 385 | jfs_error(sb, "nbytes is 0\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return -EIO; |
| 387 | } |
| 388 | |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 389 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | * Figure out how many blocks were allocated when this EA list was |
| 391 | * originally written to disk. |
| 392 | */ |
| 393 | nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage; |
| 394 | blkno = addressDXD(&ji->ea) << sbi->l2nbperpage; |
| 395 | |
| 396 | /* |
| 397 | * I have found the disk blocks which were originally used to store |
| 398 | * the FEALIST. now i loop over each contiguous block copying the |
| 399 | * data into the buffer. |
| 400 | */ |
| 401 | for (i = 0; i < nblocks; i += sbi->nbperpage) { |
| 402 | /* |
| 403 | * Determine how many bytes for this request, and round up to |
| 404 | * the nearest aggregate block size |
| 405 | */ |
| 406 | nb = min(PSIZE, nbytes); |
| 407 | bytes_to_read = |
| 408 | ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits)) |
| 409 | << sb->s_blocksize_bits; |
| 410 | |
| 411 | if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1))) |
| 412 | return -EIO; |
| 413 | |
| 414 | memcpy(cp, mp->data, nb); |
| 415 | release_metapage(mp); |
| 416 | |
| 417 | cp += PSIZE; |
| 418 | nbytes -= nb; |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * NAME: ea_get |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 426 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | * FUNCTION: Returns buffer containing existing extended attributes. |
| 428 | * The size of the buffer will be the larger of the existing |
| 429 | * attributes size, or min_size. |
| 430 | * |
| 431 | * The buffer, which may be inlined in the inode or in the |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 432 | * page cache must be release by calling ea_release or ea_put |
| 433 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | * PARAMETERS: |
| 435 | * inode - Inode pointer |
| 436 | * ea_buf - Structure to be populated with ealist and its metadata |
| 437 | * min_size- minimum size of buffer to be returned |
| 438 | * |
| 439 | * RETURNS: 0 for success; Other indicates failure |
| 440 | */ |
| 441 | static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size) |
| 442 | { |
| 443 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 444 | struct super_block *sb = inode->i_sb; |
| 445 | int size; |
| 446 | int ea_size = sizeDXD(&ji->ea); |
| 447 | int blocks_needed, current_blocks; |
| 448 | s64 blkno; |
| 449 | int rc; |
| 450 | int quota_allocation = 0; |
| 451 | |
| 452 | /* When fsck.jfs clears a bad ea, it doesn't clear the size */ |
| 453 | if (ji->ea.flag == 0) |
| 454 | ea_size = 0; |
| 455 | |
| 456 | if (ea_size == 0) { |
| 457 | if (min_size == 0) { |
| 458 | ea_buf->flag = 0; |
| 459 | ea_buf->max_size = 0; |
| 460 | ea_buf->xattr = NULL; |
| 461 | return 0; |
| 462 | } |
| 463 | if ((min_size <= sizeof (ji->i_inline_ea)) && |
| 464 | (ji->mode2 & INLINEEA)) { |
| 465 | ea_buf->flag = EA_INLINE | EA_NEW; |
| 466 | ea_buf->max_size = sizeof (ji->i_inline_ea); |
| 467 | ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea; |
| 468 | DXDlength(&ea_buf->new_ea, 0); |
| 469 | DXDaddress(&ea_buf->new_ea, 0); |
| 470 | ea_buf->new_ea.flag = DXD_INLINE; |
| 471 | DXDsize(&ea_buf->new_ea, min_size); |
| 472 | return 0; |
| 473 | } |
| 474 | current_blocks = 0; |
| 475 | } else if (ji->ea.flag & DXD_INLINE) { |
| 476 | if (min_size <= sizeof (ji->i_inline_ea)) { |
| 477 | ea_buf->flag = EA_INLINE; |
| 478 | ea_buf->max_size = sizeof (ji->i_inline_ea); |
| 479 | ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea; |
| 480 | goto size_check; |
| 481 | } |
| 482 | current_blocks = 0; |
| 483 | } else { |
| 484 | if (!(ji->ea.flag & DXD_EXTENT)) { |
Joe Perches | eb8630d | 2013-06-04 16:39:15 -0700 | [diff] [blame] | 485 | jfs_error(sb, "invalid ea.flag\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return -EIO; |
| 487 | } |
| 488 | current_blocks = (ea_size + sb->s_blocksize - 1) >> |
| 489 | sb->s_blocksize_bits; |
| 490 | } |
| 491 | size = max(min_size, ea_size); |
| 492 | |
| 493 | if (size > PSIZE) { |
| 494 | /* |
| 495 | * To keep the rest of the code simple. Allocate a |
| 496 | * contiguous buffer to work with |
| 497 | */ |
| 498 | ea_buf->xattr = kmalloc(size, GFP_KERNEL); |
| 499 | if (ea_buf->xattr == NULL) |
| 500 | return -ENOMEM; |
| 501 | |
| 502 | ea_buf->flag = EA_MALLOC; |
| 503 | ea_buf->max_size = (size + sb->s_blocksize - 1) & |
| 504 | ~(sb->s_blocksize - 1); |
| 505 | |
| 506 | if (ea_size == 0) |
| 507 | return 0; |
| 508 | |
| 509 | if ((rc = ea_read(inode, ea_buf->xattr))) { |
| 510 | kfree(ea_buf->xattr); |
| 511 | ea_buf->xattr = NULL; |
| 512 | return rc; |
| 513 | } |
| 514 | goto size_check; |
| 515 | } |
| 516 | blocks_needed = (min_size + sb->s_blocksize - 1) >> |
| 517 | sb->s_blocksize_bits; |
| 518 | |
| 519 | if (blocks_needed > current_blocks) { |
| 520 | /* Allocate new blocks to quota. */ |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 521 | rc = dquot_alloc_block(inode, blocks_needed); |
| 522 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | return -EDQUOT; |
| 524 | |
| 525 | quota_allocation = blocks_needed; |
| 526 | |
| 527 | rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed, |
| 528 | &blkno); |
| 529 | if (rc) |
| 530 | goto clean_up; |
| 531 | |
| 532 | DXDlength(&ea_buf->new_ea, blocks_needed); |
| 533 | DXDaddress(&ea_buf->new_ea, blkno); |
| 534 | ea_buf->new_ea.flag = DXD_EXTENT; |
| 535 | DXDsize(&ea_buf->new_ea, min_size); |
| 536 | |
| 537 | ea_buf->flag = EA_EXTENT | EA_NEW; |
| 538 | |
| 539 | ea_buf->mp = get_metapage(inode, blkno, |
| 540 | blocks_needed << sb->s_blocksize_bits, |
| 541 | 1); |
| 542 | if (ea_buf->mp == NULL) { |
| 543 | dbFree(inode, blkno, (s64) blocks_needed); |
| 544 | rc = -EIO; |
| 545 | goto clean_up; |
| 546 | } |
| 547 | ea_buf->xattr = ea_buf->mp->data; |
| 548 | ea_buf->max_size = (min_size + sb->s_blocksize - 1) & |
| 549 | ~(sb->s_blocksize - 1); |
| 550 | if (ea_size == 0) |
| 551 | return 0; |
| 552 | if ((rc = ea_read(inode, ea_buf->xattr))) { |
| 553 | discard_metapage(ea_buf->mp); |
| 554 | dbFree(inode, blkno, (s64) blocks_needed); |
| 555 | goto clean_up; |
| 556 | } |
| 557 | goto size_check; |
| 558 | } |
| 559 | ea_buf->flag = EA_EXTENT; |
| 560 | ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea), |
| 561 | lengthDXD(&ji->ea) << sb->s_blocksize_bits, |
| 562 | 1); |
| 563 | if (ea_buf->mp == NULL) { |
| 564 | rc = -EIO; |
| 565 | goto clean_up; |
| 566 | } |
| 567 | ea_buf->xattr = ea_buf->mp->data; |
| 568 | ea_buf->max_size = (ea_size + sb->s_blocksize - 1) & |
| 569 | ~(sb->s_blocksize - 1); |
| 570 | |
| 571 | size_check: |
| 572 | if (EALIST_SIZE(ea_buf->xattr) != ea_size) { |
| 573 | printk(KERN_ERR "ea_get: invalid extended attribute\n"); |
Dave Kleikamp | 288e4d8 | 2007-06-13 10:17:50 -0500 | [diff] [blame] | 574 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, |
| 575 | ea_buf->xattr, ea_size, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | ea_release(inode, ea_buf); |
| 577 | rc = -EIO; |
| 578 | goto clean_up; |
| 579 | } |
| 580 | |
| 581 | return ea_size; |
| 582 | |
| 583 | clean_up: |
| 584 | /* Rollback quota allocation */ |
| 585 | if (quota_allocation) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 586 | dquot_free_block(inode, quota_allocation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | return (rc); |
| 589 | } |
| 590 | |
| 591 | static void ea_release(struct inode *inode, struct ea_buffer *ea_buf) |
| 592 | { |
| 593 | if (ea_buf->flag & EA_MALLOC) |
| 594 | kfree(ea_buf->xattr); |
| 595 | else if (ea_buf->flag & EA_EXTENT) { |
| 596 | assert(ea_buf->mp); |
| 597 | release_metapage(ea_buf->mp); |
| 598 | |
| 599 | if (ea_buf->flag & EA_NEW) |
| 600 | dbFree(inode, addressDXD(&ea_buf->new_ea), |
| 601 | lengthDXD(&ea_buf->new_ea)); |
| 602 | } |
| 603 | } |
| 604 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 605 | static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf, |
| 606 | int new_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 609 | unsigned long old_blocks, new_blocks; |
| 610 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | if (new_size == 0) { |
| 613 | ea_release(inode, ea_buf); |
| 614 | ea_buf = NULL; |
| 615 | } else if (ea_buf->flag & EA_INLINE) { |
| 616 | assert(new_size <= sizeof (ji->i_inline_ea)); |
| 617 | ji->mode2 &= ~INLINEEA; |
| 618 | ea_buf->new_ea.flag = DXD_INLINE; |
| 619 | DXDsize(&ea_buf->new_ea, new_size); |
| 620 | DXDaddress(&ea_buf->new_ea, 0); |
| 621 | DXDlength(&ea_buf->new_ea, 0); |
| 622 | } else if (ea_buf->flag & EA_MALLOC) { |
| 623 | rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea); |
| 624 | kfree(ea_buf->xattr); |
| 625 | } else if (ea_buf->flag & EA_NEW) { |
| 626 | /* We have already allocated a new dxd */ |
| 627 | flush_metapage(ea_buf->mp); |
| 628 | } else { |
| 629 | /* ->xattr must point to original ea's metapage */ |
| 630 | rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea); |
| 631 | discard_metapage(ea_buf->mp); |
| 632 | } |
| 633 | if (rc) |
| 634 | return rc; |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | old_blocks = new_blocks = 0; |
| 637 | |
| 638 | if (ji->ea.flag & DXD_EXTENT) { |
| 639 | invalidate_dxd_metapages(inode, ji->ea); |
| 640 | old_blocks = lengthDXD(&ji->ea); |
| 641 | } |
| 642 | |
| 643 | if (ea_buf) { |
| 644 | txEA(tid, inode, &ji->ea, &ea_buf->new_ea); |
| 645 | if (ea_buf->new_ea.flag & DXD_EXTENT) { |
| 646 | new_blocks = lengthDXD(&ea_buf->new_ea); |
| 647 | if (ji->ea.flag & DXD_INLINE) |
| 648 | ji->mode2 |= INLINEEA; |
| 649 | } |
| 650 | ji->ea = ea_buf->new_ea; |
| 651 | } else { |
| 652 | txEA(tid, inode, &ji->ea, NULL); |
| 653 | if (ji->ea.flag & DXD_INLINE) |
| 654 | ji->mode2 |= INLINEEA; |
| 655 | ji->ea.flag = 0; |
| 656 | ji->ea.size = 0; |
| 657 | } |
| 658 | |
| 659 | /* If old blocks exist, they must be removed from quota allocation. */ |
| 660 | if (old_blocks) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 661 | dquot_free_block(inode, old_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
| 663 | inode->i_ctime = CURRENT_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 665 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* |
Dave Kleikamp | d572b87 | 2006-11-02 10:50:40 -0600 | [diff] [blame] | 669 | * Most of the permission checking is done by xattr_permission in the vfs. |
Dave Kleikamp | d572b87 | 2006-11-02 10:50:40 -0600 | [diff] [blame] | 670 | * We also need to verify that this is a namespace that we recognize. |
| 671 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | static int can_set_xattr(struct inode *inode, const char *name, |
| 673 | const void *value, size_t value_len) |
| 674 | { |
Dave Kleikamp | aca0fa3 | 2010-08-09 15:57:38 -0500 | [diff] [blame] | 675 | if (!strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)) { |
| 676 | /* |
| 677 | * This makes sure that we aren't trying to set an |
| 678 | * attribute in a different namespace by prefixing it |
| 679 | * with "os2." |
| 680 | */ |
| 681 | if (is_known_namespace(name + XATTR_OS2_PREFIX_LEN)) |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 682 | return -EOPNOTSUPP; |
Dave Kleikamp | aca0fa3 | 2010-08-09 15:57:38 -0500 | [diff] [blame] | 683 | return 0; |
| 684 | } |
| 685 | |
Christoph Hellwig | 44a0033 | 2006-01-09 20:51:57 -0800 | [diff] [blame] | 686 | /* |
| 687 | * Don't allow setting an attribute in an unknown namespace. |
| 688 | */ |
| 689 | if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) && |
| 690 | strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) && |
Dave Kleikamp | aca0fa3 | 2010-08-09 15:57:38 -0500 | [diff] [blame] | 691 | strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | return -EOPNOTSUPP; |
| 693 | |
Christoph Hellwig | 44a0033 | 2006-01-09 20:51:57 -0800 | [diff] [blame] | 694 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 697 | int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name, |
| 698 | const void *value, size_t value_len, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
| 700 | struct jfs_ea_list *ealist; |
| 701 | struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL; |
| 702 | struct ea_buffer ea_buf; |
| 703 | int old_ea_size = 0; |
| 704 | int xattr_size; |
| 705 | int new_size; |
| 706 | int namelen = strlen(name); |
| 707 | char *os2name = NULL; |
| 708 | int found = 0; |
| 709 | int rc; |
| 710 | int length; |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) { |
| 713 | os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1, |
| 714 | GFP_KERNEL); |
| 715 | if (!os2name) |
| 716 | return -ENOMEM; |
| 717 | strcpy(os2name, name + XATTR_OS2_PREFIX_LEN); |
| 718 | name = os2name; |
| 719 | namelen -= XATTR_OS2_PREFIX_LEN; |
| 720 | } |
| 721 | |
| 722 | down_write(&JFS_IP(inode)->xattr_sem); |
| 723 | |
| 724 | xattr_size = ea_get(inode, &ea_buf, 0); |
| 725 | if (xattr_size < 0) { |
| 726 | rc = xattr_size; |
| 727 | goto out; |
| 728 | } |
| 729 | |
| 730 | again: |
| 731 | ealist = (struct jfs_ea_list *) ea_buf.xattr; |
| 732 | new_size = sizeof (struct jfs_ea_list); |
| 733 | |
| 734 | if (xattr_size) { |
| 735 | for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); |
| 736 | ea = NEXT_EA(ea)) { |
| 737 | if ((namelen == ea->namelen) && |
| 738 | (memcmp(name, ea->name, namelen) == 0)) { |
| 739 | found = 1; |
| 740 | if (flags & XATTR_CREATE) { |
| 741 | rc = -EEXIST; |
| 742 | goto release; |
| 743 | } |
| 744 | old_ea = ea; |
| 745 | old_ea_size = EA_SIZE(ea); |
| 746 | next_ea = NEXT_EA(ea); |
| 747 | } else |
| 748 | new_size += EA_SIZE(ea); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (!found) { |
| 753 | if (flags & XATTR_REPLACE) { |
| 754 | rc = -ENODATA; |
| 755 | goto release; |
| 756 | } |
| 757 | if (value == NULL) { |
| 758 | rc = 0; |
| 759 | goto release; |
| 760 | } |
| 761 | } |
| 762 | if (value) |
| 763 | new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len; |
| 764 | |
| 765 | if (new_size > ea_buf.max_size) { |
| 766 | /* |
| 767 | * We need to allocate more space for merged ea list. |
| 768 | * We should only have loop to again: once. |
| 769 | */ |
| 770 | ea_release(inode, &ea_buf); |
| 771 | xattr_size = ea_get(inode, &ea_buf, new_size); |
| 772 | if (xattr_size < 0) { |
| 773 | rc = xattr_size; |
| 774 | goto out; |
| 775 | } |
| 776 | goto again; |
| 777 | } |
| 778 | |
| 779 | /* Remove old ea of the same name */ |
| 780 | if (found) { |
| 781 | /* number of bytes following target EA */ |
| 782 | length = (char *) END_EALIST(ealist) - (char *) next_ea; |
| 783 | if (length > 0) |
| 784 | memmove(old_ea, next_ea, length); |
| 785 | xattr_size -= old_ea_size; |
| 786 | } |
| 787 | |
| 788 | /* Add new entry to the end */ |
| 789 | if (value) { |
| 790 | if (xattr_size == 0) |
| 791 | /* Completely new ea list */ |
| 792 | xattr_size = sizeof (struct jfs_ea_list); |
| 793 | |
Jie Liu | 0439e09 | 2014-01-02 11:30:42 -0600 | [diff] [blame] | 794 | /* |
| 795 | * The size of EA value is limitted by on-disk format up to |
| 796 | * __le16, there would be an overflow if the size is equal |
| 797 | * to XATTR_SIZE_MAX (65536). In order to avoid this issue, |
| 798 | * we can pre-checkup the value size against USHRT_MAX, and |
| 799 | * return -E2BIG in this case, which is consistent with the |
| 800 | * VFS setxattr interface. |
| 801 | */ |
| 802 | if (value_len >= USHRT_MAX) { |
| 803 | rc = -E2BIG; |
| 804 | goto release; |
| 805 | } |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | ea = (struct jfs_ea *) ((char *) ealist + xattr_size); |
| 808 | ea->flag = 0; |
| 809 | ea->namelen = namelen; |
| 810 | ea->valuelen = (cpu_to_le16(value_len)); |
| 811 | memcpy(ea->name, name, namelen); |
| 812 | ea->name[namelen] = 0; |
| 813 | if (value_len) |
| 814 | memcpy(&ea->name[namelen + 1], value, value_len); |
| 815 | xattr_size += EA_SIZE(ea); |
| 816 | } |
| 817 | |
| 818 | /* DEBUG - If we did this right, these number match */ |
| 819 | if (xattr_size != new_size) { |
| 820 | printk(KERN_ERR |
Jie Liu | 0439e09 | 2014-01-02 11:30:42 -0600 | [diff] [blame] | 821 | "__jfs_setxattr: xattr_size = %d, new_size = %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | xattr_size, new_size); |
| 823 | |
| 824 | rc = -EINVAL; |
| 825 | goto release; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * If we're left with an empty list, there's no ea |
| 830 | */ |
| 831 | if (new_size == sizeof (struct jfs_ea_list)) |
| 832 | new_size = 0; |
| 833 | |
| 834 | ealist->size = cpu_to_le32(new_size); |
| 835 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 836 | rc = ea_put(tid, inode, &ea_buf, new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
| 838 | goto out; |
| 839 | release: |
| 840 | ea_release(inode, &ea_buf); |
| 841 | out: |
| 842 | up_write(&JFS_IP(inode)->xattr_sem); |
| 843 | |
Jesper Juhl | 259692b | 2005-05-09 10:47:14 -0500 | [diff] [blame] | 844 | kfree(os2name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
| 846 | return rc; |
| 847 | } |
| 848 | |
| 849 | int jfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 850 | size_t value_len, int flags) |
| 851 | { |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 852 | struct inode *inode = dentry->d_inode; |
| 853 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 854 | int rc; |
| 855 | tid_t tid; |
| 856 | |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 857 | /* |
| 858 | * If this is a request for a synthetic attribute in the system.* |
| 859 | * namespace use the generic infrastructure to resolve a handler |
| 860 | * for it via sb->s_xattr. |
| 861 | */ |
| 862 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 863 | return generic_setxattr(dentry, name, value, value_len, flags); |
| 864 | |
Dave Kleikamp | c18f7b5 | 2014-02-07 14:36:10 -0600 | [diff] [blame] | 865 | if ((rc = can_set_xattr(inode, name, value, value_len))) |
| 866 | return rc; |
| 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (value == NULL) { /* empty EA, do not remove */ |
| 869 | value = ""; |
| 870 | value_len = 0; |
| 871 | } |
| 872 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 873 | tid = txBegin(inode->i_sb, 0); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 874 | mutex_lock(&ji->commit_mutex); |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 875 | rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len, |
| 876 | flags); |
| 877 | if (!rc) |
| 878 | rc = txCommit(tid, 1, &inode, 0); |
| 879 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 880 | mutex_unlock(&ji->commit_mutex); |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 881 | |
| 882 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data, |
| 886 | size_t buf_size) |
| 887 | { |
| 888 | struct jfs_ea_list *ealist; |
| 889 | struct jfs_ea *ea; |
| 890 | struct ea_buffer ea_buf; |
| 891 | int xattr_size; |
| 892 | ssize_t size; |
| 893 | int namelen = strlen(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | char *value; |
| 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | down_read(&JFS_IP(inode)->xattr_sem); |
| 897 | |
| 898 | xattr_size = ea_get(inode, &ea_buf, 0); |
| 899 | |
| 900 | if (xattr_size < 0) { |
| 901 | size = xattr_size; |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | if (xattr_size == 0) |
| 906 | goto not_found; |
| 907 | |
| 908 | ealist = (struct jfs_ea_list *) ea_buf.xattr; |
| 909 | |
| 910 | /* Find the named attribute */ |
| 911 | for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) |
| 912 | if ((namelen == ea->namelen) && |
| 913 | memcmp(name, ea->name, namelen) == 0) { |
| 914 | /* Found it */ |
| 915 | size = le16_to_cpu(ea->valuelen); |
| 916 | if (!data) |
| 917 | goto release; |
| 918 | else if (size > buf_size) { |
| 919 | size = -ERANGE; |
| 920 | goto release; |
| 921 | } |
| 922 | value = ((char *) &ea->name) + ea->namelen + 1; |
| 923 | memcpy(data, value, size); |
| 924 | goto release; |
| 925 | } |
| 926 | not_found: |
| 927 | size = -ENODATA; |
| 928 | release: |
| 929 | ea_release(inode, &ea_buf); |
| 930 | out: |
| 931 | up_read(&JFS_IP(inode)->xattr_sem); |
| 932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return size; |
| 934 | } |
| 935 | |
| 936 | ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data, |
| 937 | size_t buf_size) |
| 938 | { |
| 939 | int err; |
| 940 | |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 941 | /* |
| 942 | * If this is a request for a synthetic attribute in the system.* |
| 943 | * namespace use the generic infrastructure to resolve a handler |
| 944 | * for it via sb->s_xattr. |
| 945 | */ |
| 946 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 947 | return generic_getxattr(dentry, name, data, buf_size); |
| 948 | |
Dave Kleikamp | aca0fa3 | 2010-08-09 15:57:38 -0500 | [diff] [blame] | 949 | if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) { |
| 950 | /* |
| 951 | * skip past "os2." prefix |
| 952 | */ |
| 953 | name += XATTR_OS2_PREFIX_LEN; |
| 954 | /* |
| 955 | * Don't allow retrieving properly prefixed attributes |
| 956 | * by prepending them with "os2." |
| 957 | */ |
| 958 | if (is_known_namespace(name)) |
| 959 | return -EOPNOTSUPP; |
| 960 | } |
| 961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | err = __jfs_getxattr(dentry->d_inode, name, data, buf_size); |
| 963 | |
| 964 | return err; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * No special permissions are needed to list attributes except for trusted.* |
| 969 | */ |
| 970 | static inline int can_list(struct jfs_ea *ea) |
| 971 | { |
| 972 | return (strncmp(ea->name, XATTR_TRUSTED_PREFIX, |
| 973 | XATTR_TRUSTED_PREFIX_LEN) || |
| 974 | capable(CAP_SYS_ADMIN)); |
| 975 | } |
| 976 | |
| 977 | ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size) |
| 978 | { |
| 979 | struct inode *inode = dentry->d_inode; |
| 980 | char *buffer; |
| 981 | ssize_t size = 0; |
| 982 | int xattr_size; |
| 983 | struct jfs_ea_list *ealist; |
| 984 | struct jfs_ea *ea; |
| 985 | struct ea_buffer ea_buf; |
| 986 | |
| 987 | down_read(&JFS_IP(inode)->xattr_sem); |
| 988 | |
| 989 | xattr_size = ea_get(inode, &ea_buf, 0); |
| 990 | if (xattr_size < 0) { |
| 991 | size = xattr_size; |
| 992 | goto out; |
| 993 | } |
| 994 | |
| 995 | if (xattr_size == 0) |
| 996 | goto release; |
| 997 | |
| 998 | ealist = (struct jfs_ea_list *) ea_buf.xattr; |
| 999 | |
| 1000 | /* compute required size of list */ |
| 1001 | for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) { |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 1002 | if (can_list(ea)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | size += name_size(ea) + 1; |
| 1004 | } |
| 1005 | |
| 1006 | if (!data) |
| 1007 | goto release; |
| 1008 | |
| 1009 | if (size > buf_size) { |
| 1010 | size = -ERANGE; |
| 1011 | goto release; |
| 1012 | } |
| 1013 | |
| 1014 | /* Copy attribute names to buffer */ |
| 1015 | buffer = data; |
| 1016 | for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) { |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 1017 | if (can_list(ea)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | int namelen = copy_name(buffer, ea); |
| 1019 | buffer += namelen + 1; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | release: |
| 1024 | ea_release(inode, &ea_buf); |
| 1025 | out: |
| 1026 | up_read(&JFS_IP(inode)->xattr_sem); |
| 1027 | return size; |
| 1028 | } |
| 1029 | |
| 1030 | int jfs_removexattr(struct dentry *dentry, const char *name) |
| 1031 | { |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1032 | struct inode *inode = dentry->d_inode; |
| 1033 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 1034 | int rc; |
| 1035 | tid_t tid; |
| 1036 | |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 1037 | /* |
| 1038 | * If this is a request for a synthetic attribute in the system.* |
| 1039 | * namespace use the generic infrastructure to resolve a handler |
| 1040 | * for it via sb->s_xattr. |
| 1041 | */ |
| 1042 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 1043 | return generic_removexattr(dentry, name); |
| 1044 | |
Dave Kleikamp | c18f7b5 | 2014-02-07 14:36:10 -0600 | [diff] [blame] | 1045 | if ((rc = can_set_xattr(inode, name, NULL, 0))) |
| 1046 | return rc; |
| 1047 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1048 | tid = txBegin(inode->i_sb, 0); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1049 | mutex_lock(&ji->commit_mutex); |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1050 | rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE); |
| 1051 | if (!rc) |
| 1052 | rc = txCommit(tid, 1, &inode, 0); |
| 1053 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1054 | mutex_unlock(&ji->commit_mutex); |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1055 | |
| 1056 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 1058 | |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 1059 | /* |
| 1060 | * List of handlers for synthetic system.* attributes. All real ondisk |
| 1061 | * attributes are handled directly. |
| 1062 | */ |
| 1063 | const struct xattr_handler *jfs_xattr_handlers[] = { |
Dave Kleikamp | c18f7b5 | 2014-02-07 14:36:10 -0600 | [diff] [blame] | 1064 | #ifdef CONFIG_JFS_POSIX_ACL |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 1065 | &posix_acl_access_xattr_handler, |
| 1066 | &posix_acl_default_xattr_handler, |
| 1067 | #endif |
| 1068 | NULL, |
| 1069 | }; |
| 1070 | |
| 1071 | |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 1072 | #ifdef CONFIG_JFS_SECURITY |
Dave Kleikamp | 21d1101 | 2013-06-05 14:32:08 -0500 | [diff] [blame] | 1073 | static int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array, |
| 1074 | void *fs_info) |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 1075 | { |
| 1076 | const struct xattr *xattr; |
| 1077 | tid_t *tid = fs_info; |
| 1078 | char *name; |
| 1079 | int err = 0; |
| 1080 | |
| 1081 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
| 1082 | name = kmalloc(XATTR_SECURITY_PREFIX_LEN + |
| 1083 | strlen(xattr->name) + 1, GFP_NOFS); |
| 1084 | if (!name) { |
| 1085 | err = -ENOMEM; |
| 1086 | break; |
| 1087 | } |
| 1088 | strcpy(name, XATTR_SECURITY_PREFIX); |
| 1089 | strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); |
| 1090 | |
| 1091 | err = __jfs_setxattr(*tid, inode, name, |
| 1092 | xattr->value, xattr->value_len, 0); |
| 1093 | kfree(name); |
| 1094 | if (err < 0) |
| 1095 | break; |
| 1096 | } |
| 1097 | return err; |
| 1098 | } |
| 1099 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 1100 | int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir, |
| 1101 | const struct qstr *qstr) |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 1102 | { |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 1103 | return security_inode_init_security(inode, dir, qstr, |
| 1104 | &jfs_initxattrs, &tid); |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 1105 | } |
| 1106 | #endif |