blob: a6797986b625a34d19e097050c58f582c177c30c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Kleikamp63f83c92006-10-02 09:55:27 -05007 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * (at your option) any later version.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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 Kleikamp63f83c92006-10-02 09:55:27 -050016 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/fs.h>
22#include <linux/xattr.h>
Christoph Hellwig9a59f452005-06-23 00:10:19 -070023#include <linux/posix_acl_xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/quotaops.h>
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -050026#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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 Kleikamp63f83c92006-10-02 09:55:27 -050061 * | Overall EA | First FEA Element | Second FEA Element | .....
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * | List Size | | |
63 * +------------+-------------------+--------------------+-----
64 *
65 * On-disk:
66 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -050067 * 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 Torvalds1da177e2005-04-16 15:20:36 -070070 */
71
72struct 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 Torvalds1da177e2005-04-16 15:20:36 -070088
Andreas Gruenbacher6c8f9802016-04-22 14:43:48 +020089/*
90 * Mapping of on-disk attribute names: for on-disk attribute names with an
91 * unknown prefix (not "system.", "user.", "security.", or "trusted."), the
92 * prefix "os2." is prepended. On the way back to disk, "os2." prefixes are
93 * stripped and we make sure that the remaining name does not start with one
94 * of the know prefixes.
95 */
96
Dave Kleikampaca0fa32010-08-09 15:57:38 -050097static int is_known_namespace(const char *name)
98{
99 if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
100 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
101 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
102 strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
103 return false;
104
105 return true;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static inline int name_size(struct jfs_ea *ea)
109{
Andreas Gruenbacher6c8f9802016-04-22 14:43:48 +0200110 if (is_known_namespace(ea->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return ea->namelen;
Andreas Gruenbacher6c8f9802016-04-22 14:43:48 +0200112 else
113 return ea->namelen + XATTR_OS2_PREFIX_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116static inline int copy_name(char *buffer, struct jfs_ea *ea)
117{
118 int len = ea->namelen;
119
Andreas Gruenbacher6c8f9802016-04-22 14:43:48 +0200120 if (!is_known_namespace(ea->name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
122 buffer += XATTR_OS2_PREFIX_LEN;
123 len += XATTR_OS2_PREFIX_LEN;
124 }
125 memcpy(buffer, ea->name, ea->namelen);
126 buffer[ea->namelen] = 0;
127
128 return len;
129}
130
131/* Forward references */
132static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
133
134/*
135 * NAME: ea_write_inline
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * FUNCTION: Attempt to write an EA inline if area is available
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500138 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * PRE CONDITIONS:
140 * Already verified that the specified EA is small enough to fit inline
141 *
142 * PARAMETERS:
143 * ip - Inode pointer
144 * ealist - EA list pointer
145 * size - size of ealist in bytes
146 * ea - dxd_t structure to be filled in with necessary EA information
147 * if we successfully copy the EA inline
148 *
149 * NOTES:
150 * Checks if the inode's inline area is available. If so, copies EA inline
151 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
152 * have to be put into an extent.
153 *
154 * RETURNS: 0 for successful copy to inline area; -1 if area not available
155 */
156static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
157 int size, dxd_t * ea)
158{
159 struct jfs_inode_info *ji = JFS_IP(ip);
160
161 /*
162 * Make sure we have an EA -- the NULL EA list is valid, but you
163 * can't copy it!
164 */
165 if (ealist && size > sizeof (struct jfs_ea_list)) {
166 assert(size <= sizeof (ji->i_inline_ea));
167
168 /*
169 * See if the space is available or if it is already being
170 * used for an inline EA.
171 */
172 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
173 return -EPERM;
174
175 DXDsize(ea, size);
176 DXDlength(ea, 0);
177 DXDaddress(ea, 0);
178 memcpy(ji->i_inline_ea, ealist, size);
179 ea->flag = DXD_INLINE;
180 ji->mode2 &= ~INLINEEA;
181 } else {
182 ea->flag = 0;
183 DXDsize(ea, 0);
184 DXDlength(ea, 0);
185 DXDaddress(ea, 0);
186
187 /* Free up INLINE area */
188 if (ji->ea.flag & DXD_INLINE)
189 ji->mode2 |= INLINEEA;
190 }
191
192 return 0;
193}
194
195/*
196 * NAME: ea_write
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * FUNCTION: Write an EA for an inode
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500199 *
200 * PRE CONDITIONS: EA has been verified
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
202 * PARAMETERS:
203 * ip - Inode pointer
204 * ealist - EA list pointer
205 * size - size of ealist in bytes
206 * ea - dxd_t structure to be filled in appropriately with where the
207 * EA was copied
208 *
209 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
210 * extent and synchronously writes it to those blocks.
211 *
212 * RETURNS: 0 for success; Anything else indicates failure
213 */
214static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
215 dxd_t * ea)
216{
217 struct super_block *sb = ip->i_sb;
218 struct jfs_inode_info *ji = JFS_IP(ip);
219 struct jfs_sb_info *sbi = JFS_SBI(sb);
220 int nblocks;
221 s64 blkno;
222 int rc = 0, i;
223 char *cp;
224 s32 nbytes, nb;
225 s32 bytes_to_write;
226 struct metapage *mp;
227
228 /*
229 * Quick check to see if this is an in-linable EA. Short EAs
230 * and empty EAs are all in-linable, provided the space exists.
231 */
232 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
233 if (!ea_write_inline(ip, ealist, size, ea))
234 return 0;
235 }
236
237 /* figure out how many blocks we need */
238 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
239
240 /* Allocate new blocks to quota. */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500241 rc = dquot_alloc_block(ip, nblocks);
242 if (rc)
243 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
246 if (rc) {
247 /*Rollback quota allocation. */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500248 dquot_free_block(ip, nblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return rc;
250 }
251
252 /*
253 * Now have nblocks worth of storage to stuff into the FEALIST.
254 * loop over the FEALIST copying data into the buffer one page at
255 * a time.
256 */
257 cp = (char *) ealist;
258 nbytes = size;
259 for (i = 0; i < nblocks; i += sbi->nbperpage) {
260 /*
261 * Determine how many bytes for this request, and round up to
262 * the nearest aggregate block size
263 */
264 nb = min(PSIZE, nbytes);
265 bytes_to_write =
266 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
267 << sb->s_blocksize_bits;
268
269 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
270 rc = -EIO;
271 goto failed;
272 }
273
274 memcpy(mp->data, cp, nb);
275
276 /*
277 * We really need a way to propagate errors for
278 * forced writes like this one. --hch
279 *
280 * (__write_metapage => release_metapage => flush_metapage)
281 */
282#ifdef _JFS_FIXME
283 if ((rc = flush_metapage(mp))) {
284 /*
285 * the write failed -- this means that the buffer
286 * is still assigned and the blocks are not being
287 * used. this seems like the best error recovery
288 * we can get ...
289 */
290 goto failed;
291 }
292#else
293 flush_metapage(mp);
294#endif
295
296 cp += PSIZE;
297 nbytes -= nb;
298 }
299
300 ea->flag = DXD_EXTENT;
301 DXDsize(ea, le32_to_cpu(ealist->size));
302 DXDlength(ea, nblocks);
303 DXDaddress(ea, blkno);
304
305 /* Free up INLINE area */
306 if (ji->ea.flag & DXD_INLINE)
307 ji->mode2 |= INLINEEA;
308
309 return 0;
310
311 failed:
312 /* Rollback quota allocation. */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500313 dquot_free_block(ip, nblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 dbFree(ip, blkno, nblocks);
316 return rc;
317}
318
319/*
320 * NAME: ea_read_inline
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500321 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * FUNCTION: Read an inlined EA into user's buffer
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500323 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * PARAMETERS:
325 * ip - Inode pointer
326 * ealist - Pointer to buffer to fill in with EA
327 *
328 * RETURNS: 0
329 */
330static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
331{
332 struct jfs_inode_info *ji = JFS_IP(ip);
333 int ea_size = sizeDXD(&ji->ea);
334
335 if (ea_size == 0) {
336 ealist->size = 0;
337 return 0;
338 }
339
340 /* Sanity Check */
341 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
342 return -EIO;
343 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
344 != ea_size)
345 return -EIO;
346
347 memcpy(ealist, ji->i_inline_ea, ea_size);
348 return 0;
349}
350
351/*
352 * NAME: ea_read
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * FUNCTION: copy EA data into user's buffer
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500355 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * PARAMETERS:
357 * ip - Inode pointer
358 * ealist - Pointer to buffer to fill in with EA
359 *
360 * NOTES: If EA is inline calls ea_read_inline() to copy EA.
361 *
362 * RETURNS: 0 for success; other indicates failure
363 */
364static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
365{
366 struct super_block *sb = ip->i_sb;
367 struct jfs_inode_info *ji = JFS_IP(ip);
368 struct jfs_sb_info *sbi = JFS_SBI(sb);
369 int nblocks;
370 s64 blkno;
371 char *cp = (char *) ealist;
372 int i;
373 int nbytes, nb;
374 s32 bytes_to_read;
375 struct metapage *mp;
376
377 /* quick check for in-line EA */
378 if (ji->ea.flag & DXD_INLINE)
379 return ea_read_inline(ip, ealist);
380
381 nbytes = sizeDXD(&ji->ea);
382 if (!nbytes) {
Joe Percheseb8630d2013-06-04 16:39:15 -0700383 jfs_error(sb, "nbytes is 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EIO;
385 }
386
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500387 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * Figure out how many blocks were allocated when this EA list was
389 * originally written to disk.
390 */
391 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
392 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
393
394 /*
395 * I have found the disk blocks which were originally used to store
396 * the FEALIST. now i loop over each contiguous block copying the
397 * data into the buffer.
398 */
399 for (i = 0; i < nblocks; i += sbi->nbperpage) {
400 /*
401 * Determine how many bytes for this request, and round up to
402 * the nearest aggregate block size
403 */
404 nb = min(PSIZE, nbytes);
405 bytes_to_read =
406 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
407 << sb->s_blocksize_bits;
408
409 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
410 return -EIO;
411
412 memcpy(cp, mp->data, nb);
413 release_metapage(mp);
414
415 cp += PSIZE;
416 nbytes -= nb;
417 }
418
419 return 0;
420}
421
422/*
423 * NAME: ea_get
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * FUNCTION: Returns buffer containing existing extended attributes.
426 * The size of the buffer will be the larger of the existing
427 * attributes size, or min_size.
428 *
429 * The buffer, which may be inlined in the inode or in the
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500430 * page cache must be release by calling ea_release or ea_put
431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * PARAMETERS:
433 * inode - Inode pointer
434 * ea_buf - Structure to be populated with ealist and its metadata
435 * min_size- minimum size of buffer to be returned
436 *
437 * RETURNS: 0 for success; Other indicates failure
438 */
439static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
440{
441 struct jfs_inode_info *ji = JFS_IP(inode);
442 struct super_block *sb = inode->i_sb;
443 int size;
444 int ea_size = sizeDXD(&ji->ea);
445 int blocks_needed, current_blocks;
446 s64 blkno;
447 int rc;
448 int quota_allocation = 0;
449
450 /* When fsck.jfs clears a bad ea, it doesn't clear the size */
451 if (ji->ea.flag == 0)
452 ea_size = 0;
453
454 if (ea_size == 0) {
455 if (min_size == 0) {
456 ea_buf->flag = 0;
457 ea_buf->max_size = 0;
458 ea_buf->xattr = NULL;
459 return 0;
460 }
461 if ((min_size <= sizeof (ji->i_inline_ea)) &&
462 (ji->mode2 & INLINEEA)) {
463 ea_buf->flag = EA_INLINE | EA_NEW;
464 ea_buf->max_size = sizeof (ji->i_inline_ea);
465 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
466 DXDlength(&ea_buf->new_ea, 0);
467 DXDaddress(&ea_buf->new_ea, 0);
468 ea_buf->new_ea.flag = DXD_INLINE;
469 DXDsize(&ea_buf->new_ea, min_size);
470 return 0;
471 }
472 current_blocks = 0;
473 } else if (ji->ea.flag & DXD_INLINE) {
474 if (min_size <= sizeof (ji->i_inline_ea)) {
475 ea_buf->flag = EA_INLINE;
476 ea_buf->max_size = sizeof (ji->i_inline_ea);
477 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
478 goto size_check;
479 }
480 current_blocks = 0;
481 } else {
482 if (!(ji->ea.flag & DXD_EXTENT)) {
Joe Percheseb8630d2013-06-04 16:39:15 -0700483 jfs_error(sb, "invalid ea.flag\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -EIO;
485 }
486 current_blocks = (ea_size + sb->s_blocksize - 1) >>
487 sb->s_blocksize_bits;
488 }
489 size = max(min_size, ea_size);
490
491 if (size > PSIZE) {
492 /*
493 * To keep the rest of the code simple. Allocate a
Shankara Pailoor240d4652018-06-05 08:33:27 -0500494 * contiguous buffer to work with. Make the buffer large
495 * enough to make use of the whole extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
Shankara Pailoor240d4652018-06-05 08:33:27 -0500497 ea_buf->max_size = (size + sb->s_blocksize - 1) &
498 ~(sb->s_blocksize - 1);
499
500 ea_buf->xattr = kmalloc(ea_buf->max_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (ea_buf->xattr == NULL)
502 return -ENOMEM;
503
504 ea_buf->flag = EA_MALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
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 Hellwig5dd40562010-03-03 09:05:00 -0500521 rc = dquot_alloc_block(inode, blocks_needed);
522 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 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 Kleikamp288e4d82007-06-13 10:17:50 -0500574 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
575 ea_buf->xattr, ea_size, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 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 Hellwig5dd40562010-03-03 09:05:00 -0500586 dquot_free_block(inode, quota_allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 return (rc);
589}
590
591static 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 Kleikamp4f4b4012005-09-01 09:02:43 -0500605static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
606 int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct jfs_inode_info *ji = JFS_IP(inode);
609 unsigned long old_blocks, new_blocks;
610 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
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 Torvalds1da177e2005-04-16 15:20:36 -0700636 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 Hellwig5dd40562010-03-03 09:05:00 -0500661 dquot_free_block(inode, old_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Deepa Dinamani078cd822016-09-14 07:48:04 -0700663 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500668int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
669 const void *value, size_t value_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct jfs_ea_list *ealist;
672 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
673 struct ea_buffer ea_buf;
674 int old_ea_size = 0;
675 int xattr_size;
676 int new_size;
677 int namelen = strlen(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int found = 0;
679 int rc;
680 int length;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 down_write(&JFS_IP(inode)->xattr_sem);
683
684 xattr_size = ea_get(inode, &ea_buf, 0);
685 if (xattr_size < 0) {
686 rc = xattr_size;
687 goto out;
688 }
689
690 again:
691 ealist = (struct jfs_ea_list *) ea_buf.xattr;
692 new_size = sizeof (struct jfs_ea_list);
693
694 if (xattr_size) {
695 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
696 ea = NEXT_EA(ea)) {
697 if ((namelen == ea->namelen) &&
698 (memcmp(name, ea->name, namelen) == 0)) {
699 found = 1;
700 if (flags & XATTR_CREATE) {
701 rc = -EEXIST;
702 goto release;
703 }
704 old_ea = ea;
705 old_ea_size = EA_SIZE(ea);
706 next_ea = NEXT_EA(ea);
707 } else
708 new_size += EA_SIZE(ea);
709 }
710 }
711
712 if (!found) {
713 if (flags & XATTR_REPLACE) {
714 rc = -ENODATA;
715 goto release;
716 }
717 if (value == NULL) {
718 rc = 0;
719 goto release;
720 }
721 }
722 if (value)
723 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
724
725 if (new_size > ea_buf.max_size) {
726 /*
727 * We need to allocate more space for merged ea list.
728 * We should only have loop to again: once.
729 */
730 ea_release(inode, &ea_buf);
731 xattr_size = ea_get(inode, &ea_buf, new_size);
732 if (xattr_size < 0) {
733 rc = xattr_size;
734 goto out;
735 }
736 goto again;
737 }
738
739 /* Remove old ea of the same name */
740 if (found) {
741 /* number of bytes following target EA */
742 length = (char *) END_EALIST(ealist) - (char *) next_ea;
743 if (length > 0)
744 memmove(old_ea, next_ea, length);
745 xattr_size -= old_ea_size;
746 }
747
748 /* Add new entry to the end */
749 if (value) {
750 if (xattr_size == 0)
751 /* Completely new ea list */
752 xattr_size = sizeof (struct jfs_ea_list);
753
Jie Liu0439e092014-01-02 11:30:42 -0600754 /*
755 * The size of EA value is limitted by on-disk format up to
756 * __le16, there would be an overflow if the size is equal
757 * to XATTR_SIZE_MAX (65536). In order to avoid this issue,
758 * we can pre-checkup the value size against USHRT_MAX, and
759 * return -E2BIG in this case, which is consistent with the
760 * VFS setxattr interface.
761 */
762 if (value_len >= USHRT_MAX) {
763 rc = -E2BIG;
764 goto release;
765 }
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
768 ea->flag = 0;
769 ea->namelen = namelen;
770 ea->valuelen = (cpu_to_le16(value_len));
771 memcpy(ea->name, name, namelen);
772 ea->name[namelen] = 0;
773 if (value_len)
774 memcpy(&ea->name[namelen + 1], value, value_len);
775 xattr_size += EA_SIZE(ea);
776 }
777
778 /* DEBUG - If we did this right, these number match */
779 if (xattr_size != new_size) {
780 printk(KERN_ERR
Jie Liu0439e092014-01-02 11:30:42 -0600781 "__jfs_setxattr: xattr_size = %d, new_size = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 xattr_size, new_size);
783
784 rc = -EINVAL;
785 goto release;
786 }
787
788 /*
789 * If we're left with an empty list, there's no ea
790 */
791 if (new_size == sizeof (struct jfs_ea_list))
792 new_size = 0;
793
794 ealist->size = cpu_to_le32(new_size);
795
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500796 rc = ea_put(tid, inode, &ea_buf, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 goto out;
799 release:
800 ea_release(inode, &ea_buf);
801 out:
802 up_write(&JFS_IP(inode)->xattr_sem);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return rc;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
808 size_t buf_size)
809{
810 struct jfs_ea_list *ealist;
811 struct jfs_ea *ea;
812 struct ea_buffer ea_buf;
813 int xattr_size;
814 ssize_t size;
815 int namelen = strlen(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 char *value;
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 down_read(&JFS_IP(inode)->xattr_sem);
819
820 xattr_size = ea_get(inode, &ea_buf, 0);
821
822 if (xattr_size < 0) {
823 size = xattr_size;
824 goto out;
825 }
826
827 if (xattr_size == 0)
828 goto not_found;
829
830 ealist = (struct jfs_ea_list *) ea_buf.xattr;
831
832 /* Find the named attribute */
833 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
834 if ((namelen == ea->namelen) &&
835 memcmp(name, ea->name, namelen) == 0) {
836 /* Found it */
837 size = le16_to_cpu(ea->valuelen);
838 if (!data)
839 goto release;
840 else if (size > buf_size) {
841 size = -ERANGE;
842 goto release;
843 }
844 value = ((char *) &ea->name) + ea->namelen + 1;
845 memcpy(data, value, size);
846 goto release;
847 }
848 not_found:
849 size = -ENODATA;
850 release:
851 ea_release(inode, &ea_buf);
852 out:
853 up_read(&JFS_IP(inode)->xattr_sem);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return size;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*
859 * No special permissions are needed to list attributes except for trusted.*
860 */
861static inline int can_list(struct jfs_ea *ea)
862{
863 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
864 XATTR_TRUSTED_PREFIX_LEN) ||
865 capable(CAP_SYS_ADMIN));
866}
867
868ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
869{
David Howells2b0143b2015-03-17 22:25:59 +0000870 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 char *buffer;
872 ssize_t size = 0;
873 int xattr_size;
874 struct jfs_ea_list *ealist;
875 struct jfs_ea *ea;
876 struct ea_buffer ea_buf;
877
878 down_read(&JFS_IP(inode)->xattr_sem);
879
880 xattr_size = ea_get(inode, &ea_buf, 0);
881 if (xattr_size < 0) {
882 size = xattr_size;
883 goto out;
884 }
885
886 if (xattr_size == 0)
887 goto release;
888
889 ealist = (struct jfs_ea_list *) ea_buf.xattr;
890
891 /* compute required size of list */
892 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500893 if (can_list(ea))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 size += name_size(ea) + 1;
895 }
896
897 if (!data)
898 goto release;
899
900 if (size > buf_size) {
901 size = -ERANGE;
902 goto release;
903 }
904
905 /* Copy attribute names to buffer */
906 buffer = data;
907 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500908 if (can_list(ea)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int namelen = copy_name(buffer, ea);
910 buffer += namelen + 1;
911 }
912 }
913
914 release:
915 ea_release(inode, &ea_buf);
916 out:
917 up_read(&JFS_IP(inode)->xattr_sem);
918 return size;
919}
920
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200921static int __jfs_xattr_set(struct inode *inode, const char *name,
922 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500924 struct jfs_inode_info *ji = JFS_IP(inode);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500925 tid_t tid;
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200926 int rc;
Dave Kleikampc18f7b52014-02-07 14:36:10 -0600927
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500928 tid = txBegin(inode->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600929 mutex_lock(&ji->commit_mutex);
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200930 rc = __jfs_setxattr(tid, inode, name, value, size, flags);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500931 if (!rc)
932 rc = txCommit(tid, 1, &inode, 0);
933 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600934 mutex_unlock(&ji->commit_mutex);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500935
936 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500938
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200939static int jfs_xattr_get(const struct xattr_handler *handler,
940 struct dentry *unused, struct inode *inode,
941 const char *name, void *value, size_t size)
942{
943 name = xattr_full_name(handler, name);
944 return __jfs_getxattr(inode, name, value, size);
945}
946
947static int jfs_xattr_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -0400948 struct dentry *unused, struct inode *inode,
949 const char *name, const void *value,
950 size_t size, int flags)
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200951{
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200952 name = xattr_full_name(handler, name);
953 return __jfs_xattr_set(inode, name, value, size, flags);
954}
955
956static int jfs_xattr_get_os2(const struct xattr_handler *handler,
957 struct dentry *unused, struct inode *inode,
958 const char *name, void *value, size_t size)
959{
960 if (is_known_namespace(name))
961 return -EOPNOTSUPP;
962 return __jfs_getxattr(inode, name, value, size);
963}
964
965static int jfs_xattr_set_os2(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -0400966 struct dentry *unused, struct inode *inode,
967 const char *name, const void *value,
968 size_t size, int flags)
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200969{
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +0200970 if (is_known_namespace(name))
971 return -EOPNOTSUPP;
972 return __jfs_xattr_set(inode, name, value, size, flags);
973}
974
975static const struct xattr_handler jfs_user_xattr_handler = {
976 .prefix = XATTR_USER_PREFIX,
977 .get = jfs_xattr_get,
978 .set = jfs_xattr_set,
979};
980
981static const struct xattr_handler jfs_os2_xattr_handler = {
982 .prefix = XATTR_OS2_PREFIX,
983 .get = jfs_xattr_get_os2,
984 .set = jfs_xattr_set_os2,
985};
986
987static const struct xattr_handler jfs_security_xattr_handler = {
988 .prefix = XATTR_SECURITY_PREFIX,
989 .get = jfs_xattr_get,
990 .set = jfs_xattr_set,
991};
992
993static const struct xattr_handler jfs_trusted_xattr_handler = {
994 .prefix = XATTR_TRUSTED_PREFIX,
995 .get = jfs_xattr_get,
996 .set = jfs_xattr_set,
997};
998
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800999const struct xattr_handler *jfs_xattr_handlers[] = {
Dave Kleikampc18f7b52014-02-07 14:36:10 -06001000#ifdef CONFIG_JFS_POSIX_ACL
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08001001 &posix_acl_access_xattr_handler,
1002 &posix_acl_default_xattr_handler,
1003#endif
Andreas Gruenbacherc8b60562016-04-22 14:43:49 +02001004 &jfs_os2_xattr_handler,
1005 &jfs_user_xattr_handler,
1006 &jfs_security_xattr_handler,
1007 &jfs_trusted_xattr_handler,
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08001008 NULL,
1009};
1010
1011
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001012#ifdef CONFIG_JFS_SECURITY
Dave Kleikamp21d11012013-06-05 14:32:08 -05001013static int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
1014 void *fs_info)
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001015{
1016 const struct xattr *xattr;
1017 tid_t *tid = fs_info;
1018 char *name;
1019 int err = 0;
1020
1021 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1022 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
1023 strlen(xattr->name) + 1, GFP_NOFS);
1024 if (!name) {
1025 err = -ENOMEM;
1026 break;
1027 }
1028 strcpy(name, XATTR_SECURITY_PREFIX);
1029 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
1030
1031 err = __jfs_setxattr(*tid, inode, name,
1032 xattr->value, xattr->value_len, 0);
1033 kfree(name);
1034 if (err < 0)
1035 break;
1036 }
1037 return err;
1038}
1039
Eric Paris2a7dba32011-02-01 11:05:39 -05001040int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
1041 const struct qstr *qstr)
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001042{
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001043 return security_inode_init_security(inode, dir, qstr,
1044 &jfs_initxattrs, &tid);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001045}
1046#endif