Blame view

fs/jfs/xattr.c 25.1 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
  /*
   *   Copyright (C) International Business Machines  Corp., 2000-2004
   *   Copyright (C) Christoph Hellwig, 2002
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
   */
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
6
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
  #include <linux/fs.h>
  #include <linux/xattr.h>
9a59f452a   Christoph Hellwig   [PATCH] remove <l...
9
  #include <linux/posix_acl_xattr.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
10
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
  #include <linux/quotaops.h>
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
12
  #include <linux/security.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include "jfs_incore.h"
  #include "jfs_superblock.h"
  #include "jfs_dmap.h"
  #include "jfs_debug.h"
  #include "jfs_dinode.h"
  #include "jfs_extent.h"
  #include "jfs_metapage.h"
  #include "jfs_xattr.h"
  #include "jfs_acl.h"
  
  /*
   *	jfs_xattr.c: extended attribute service
   *
   * Overall design --
   *
   * Format:
   *
   *   Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
   *   value) and a variable (0 or more) number of extended attribute
   *   entries.  Each extended attribute entry (jfs_ea) is a <name,value> double
   *   where <name> is constructed from a null-terminated ascii string
   *   (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
   *   (1 ... 65535 bytes).  The in-memory format is
   *
   *   0       1        2        4                4 + namelen + 1
   *   +-------+--------+--------+----------------+-------------------+
   *   | Flags | Name   | Value  | Name String \0 | Data . . . .      |
   *   |       | Length | Length |                |                   |
   *   +-------+--------+--------+----------------+-------------------+
   *
   *   A jfs_ea_list then is structured as
   *
   *   0            4                   4 + EA_SIZE(ea1)
   *   +------------+-------------------+--------------------+-----
63f83c9fc   Dave Kleikamp   JFS: White space ...
47
   *   | Overall EA | First FEA Element | Second FEA Element | .....
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
51
52
   *   | List Size  |                   |                    |
   *   +------------+-------------------+--------------------+-----
   *
   *   On-disk:
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
53
54
55
   *	FEALISTs are stored on disk using blocks allocated by dbAlloc() and
   *	written directly. An EA list may be in-lined in the inode if there is
   *	sufficient room available.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
   */
  
  struct ea_buffer {
  	int flag;		/* Indicates what storage xattr points to */
  	int max_size;		/* largest xattr that fits in current buffer */
  	dxd_t new_ea;		/* dxd to replace ea when modifying xattr */
  	struct metapage *mp;	/* metapage containing ea list */
  	struct jfs_ea_list *xattr;	/* buffer containing ea list */
  };
  
  /*
   * ea_buffer.flag values
   */
  #define EA_INLINE	0x0001
  #define EA_EXTENT	0x0002
  #define EA_NEW		0x0004
  #define EA_MALLOC	0x0008
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73

6c8f980c7   Andreas Gruenbacher   jfs: Clean up xat...
74
75
76
77
78
79
80
  /*
   * Mapping of on-disk attribute names: for on-disk attribute names with an
   * unknown prefix (not "system.", "user.", "security.", or "trusted."), the
   * prefix "os2." is prepended.  On the way back to disk, "os2." prefixes are
   * stripped and we make sure that the remaining name does not start with one
   * of the know prefixes.
   */
aca0fa34b   Dave Kleikamp   jfs: don't allow ...
81
82
83
84
85
86
87
88
89
90
  static int is_known_namespace(const char *name)
  {
  	if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
  	    strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
  	    strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
  	    strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
  		return false;
  
  	return true;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
  static inline int name_size(struct jfs_ea *ea)
  {
6c8f980c7   Andreas Gruenbacher   jfs: Clean up xat...
93
  	if (is_known_namespace(ea->name))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  		return ea->namelen;
6c8f980c7   Andreas Gruenbacher   jfs: Clean up xat...
95
96
  	else
  		return ea->namelen + XATTR_OS2_PREFIX_LEN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
100
101
  }
  
  static inline int copy_name(char *buffer, struct jfs_ea *ea)
  {
  	int len = ea->namelen;
6c8f980c7   Andreas Gruenbacher   jfs: Clean up xat...
102
  	if (!is_known_namespace(ea->name)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  		memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
  		buffer += XATTR_OS2_PREFIX_LEN;
  		len += XATTR_OS2_PREFIX_LEN;
  	}
  	memcpy(buffer, ea->name, ea->namelen);
  	buffer[ea->namelen] = 0;
  
  	return len;
  }
  
  /* Forward references */
  static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
  
  /*
   * NAME: ea_write_inline
63f83c9fc   Dave Kleikamp   JFS: White space ...
118
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
   * FUNCTION: Attempt to write an EA inline if area is available
63f83c9fc   Dave Kleikamp   JFS: White space ...
120
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
   * PRE CONDITIONS:
   *	Already verified that the specified EA is small enough to fit inline
   *
   * PARAMETERS:
   *	ip	- Inode pointer
   *	ealist	- EA list pointer
   *	size	- size of ealist in bytes
   *	ea	- dxd_t structure to be filled in with necessary EA information
   *		  if we successfully copy the EA inline
   *
   * NOTES:
   *	Checks if the inode's inline area is available.  If so, copies EA inline
   *	and sets <ea> fields appropriately.  Otherwise, returns failure, EA will
   *	have to be put into an extent.
   *
   * RETURNS: 0 for successful copy to inline area; -1 if area not available
   */
  static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
  			   int size, dxd_t * ea)
  {
  	struct jfs_inode_info *ji = JFS_IP(ip);
  
  	/*
  	 * Make sure we have an EA -- the NULL EA list is valid, but you
  	 * can't copy it!
  	 */
  	if (ealist && size > sizeof (struct jfs_ea_list)) {
  		assert(size <= sizeof (ji->i_inline_ea));
  
  		/*
  		 * See if the space is available or if it is already being
  		 * used for an inline EA.
  		 */
  		if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
  			return -EPERM;
  
  		DXDsize(ea, size);
  		DXDlength(ea, 0);
  		DXDaddress(ea, 0);
  		memcpy(ji->i_inline_ea, ealist, size);
  		ea->flag = DXD_INLINE;
  		ji->mode2 &= ~INLINEEA;
  	} else {
  		ea->flag = 0;
  		DXDsize(ea, 0);
  		DXDlength(ea, 0);
  		DXDaddress(ea, 0);
  
  		/* Free up INLINE area */
  		if (ji->ea.flag & DXD_INLINE)
  			ji->mode2 |= INLINEEA;
  	}
  
  	return 0;
  }
  
  /*
   * NAME: ea_write
63f83c9fc   Dave Kleikamp   JFS: White space ...
179
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
   * FUNCTION: Write an EA for an inode
63f83c9fc   Dave Kleikamp   JFS: White space ...
181
182
   *
   * PRE CONDITIONS: EA has been verified
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
   *
   * PARAMETERS:
   *	ip	- Inode pointer
   *	ealist	- EA list pointer
   *	size	- size of ealist in bytes
   *	ea	- dxd_t structure to be filled in appropriately with where the
   *		  EA was copied
   *
   * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
   *	extent and synchronously writes it to those blocks.
   *
   * RETURNS: 0 for success; Anything else indicates failure
   */
  static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
  		       dxd_t * ea)
  {
  	struct super_block *sb = ip->i_sb;
  	struct jfs_inode_info *ji = JFS_IP(ip);
  	struct jfs_sb_info *sbi = JFS_SBI(sb);
  	int nblocks;
  	s64 blkno;
  	int rc = 0, i;
  	char *cp;
  	s32 nbytes, nb;
  	s32 bytes_to_write;
  	struct metapage *mp;
  
  	/*
  	 * Quick check to see if this is an in-linable EA.  Short EAs
  	 * and empty EAs are all in-linable, provided the space exists.
  	 */
  	if (!ealist || size <= sizeof (ji->i_inline_ea)) {
  		if (!ea_write_inline(ip, ealist, size, ea))
  			return 0;
  	}
  
  	/* figure out how many blocks we need */
  	nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
  
  	/* Allocate new blocks to quota. */
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
223
224
225
  	rc = dquot_alloc_block(ip, nblocks);
  	if (rc)
  		return rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
228
229
  
  	rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
  	if (rc) {
  		/*Rollback quota allocation. */
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
230
  		dquot_free_block(ip, nblocks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  		return rc;
  	}
  
  	/*
  	 * Now have nblocks worth of storage to stuff into the FEALIST.
  	 * loop over the FEALIST copying data into the buffer one page at
  	 * a time.
  	 */
  	cp = (char *) ealist;
  	nbytes = size;
  	for (i = 0; i < nblocks; i += sbi->nbperpage) {
  		/*
  		 * Determine how many bytes for this request, and round up to
  		 * the nearest aggregate block size
  		 */
  		nb = min(PSIZE, nbytes);
  		bytes_to_write =
  		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
  		    << sb->s_blocksize_bits;
  
  		if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
  			rc = -EIO;
  			goto failed;
  		}
  
  		memcpy(mp->data, cp, nb);
  
  		/*
  		 * We really need a way to propagate errors for
  		 * forced writes like this one.  --hch
  		 *
  		 * (__write_metapage => release_metapage => flush_metapage)
  		 */
  #ifdef _JFS_FIXME
  		if ((rc = flush_metapage(mp))) {
  			/*
  			 * the write failed -- this means that the buffer
  			 * is still assigned and the blocks are not being
  			 * used.  this seems like the best error recovery
  			 * we can get ...
  			 */
  			goto failed;
  		}
  #else
  		flush_metapage(mp);
  #endif
  
  		cp += PSIZE;
  		nbytes -= nb;
  	}
  
  	ea->flag = DXD_EXTENT;
  	DXDsize(ea, le32_to_cpu(ealist->size));
  	DXDlength(ea, nblocks);
  	DXDaddress(ea, blkno);
  
  	/* Free up INLINE area */
  	if (ji->ea.flag & DXD_INLINE)
  		ji->mode2 |= INLINEEA;
  
  	return 0;
  
        failed:
  	/* Rollback quota allocation. */
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
295
  	dquot_free_block(ip, nblocks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
297
298
299
300
301
302
  
  	dbFree(ip, blkno, nblocks);
  	return rc;
  }
  
  /*
   * NAME: ea_read_inline
63f83c9fc   Dave Kleikamp   JFS: White space ...
303
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
   * FUNCTION: Read an inlined EA into user's buffer
63f83c9fc   Dave Kleikamp   JFS: White space ...
305
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
   * PARAMETERS:
   *	ip	- Inode pointer
   *	ealist	- Pointer to buffer to fill in with EA
   *
   * RETURNS: 0
   */
  static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
  {
  	struct jfs_inode_info *ji = JFS_IP(ip);
  	int ea_size = sizeDXD(&ji->ea);
  
  	if (ea_size == 0) {
  		ealist->size = 0;
  		return 0;
  	}
  
  	/* Sanity Check */
  	if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
  		return -EIO;
  	if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
  	    != ea_size)
  		return -EIO;
  
  	memcpy(ealist, ji->i_inline_ea, ea_size);
  	return 0;
  }
  
  /*
   * NAME: ea_read
63f83c9fc   Dave Kleikamp   JFS: White space ...
335
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
   * FUNCTION: copy EA data into user's buffer
63f83c9fc   Dave Kleikamp   JFS: White space ...
337
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
   * PARAMETERS:
   *	ip	- Inode pointer
   *	ealist	- Pointer to buffer to fill in with EA
   *
   * NOTES:  If EA is inline calls ea_read_inline() to copy EA.
   *
   * RETURNS: 0 for success; other indicates failure
   */
  static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
  {
  	struct super_block *sb = ip->i_sb;
  	struct jfs_inode_info *ji = JFS_IP(ip);
  	struct jfs_sb_info *sbi = JFS_SBI(sb);
  	int nblocks;
  	s64 blkno;
  	char *cp = (char *) ealist;
  	int i;
  	int nbytes, nb;
  	s32 bytes_to_read;
  	struct metapage *mp;
  
  	/* quick check for in-line EA */
  	if (ji->ea.flag & DXD_INLINE)
  		return ea_read_inline(ip, ealist);
  
  	nbytes = sizeDXD(&ji->ea);
  	if (!nbytes) {
eb8630d7d   Joe Perches   jfs: Update jfs_e...
365
366
  		jfs_error(sb, "nbytes is 0
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
  		return -EIO;
  	}
63f83c9fc   Dave Kleikamp   JFS: White space ...
369
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  	 * Figure out how many blocks were allocated when this EA list was
  	 * originally written to disk.
  	 */
  	nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
  	blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
  
  	/*
  	 * I have found the disk blocks which were originally used to store
  	 * the FEALIST.  now i loop over each contiguous block copying the
  	 * data into the buffer.
  	 */
  	for (i = 0; i < nblocks; i += sbi->nbperpage) {
  		/*
  		 * Determine how many bytes for this request, and round up to
  		 * the nearest aggregate block size
  		 */
  		nb = min(PSIZE, nbytes);
  		bytes_to_read =
  		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
  		    << sb->s_blocksize_bits;
  
  		if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
  			return -EIO;
  
  		memcpy(cp, mp->data, nb);
  		release_metapage(mp);
  
  		cp += PSIZE;
  		nbytes -= nb;
  	}
  
  	return 0;
  }
  
  /*
   * NAME: ea_get
63f83c9fc   Dave Kleikamp   JFS: White space ...
406
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
409
410
411
   * FUNCTION: Returns buffer containing existing extended attributes.
   *	     The size of the buffer will be the larger of the existing
   *	     attributes size, or min_size.
   *
   *	     The buffer, which may be inlined in the inode or in the
63f83c9fc   Dave Kleikamp   JFS: White space ...
412
413
   *	     page cache must be release by calling ea_release or ea_put
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
   * PARAMETERS:
   *	inode	- Inode pointer
   *	ea_buf	- Structure to be populated with ealist and its metadata
   *	min_size- minimum size of buffer to be returned
   *
   * RETURNS: 0 for success; Other indicates failure
   */
  static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
  {
  	struct jfs_inode_info *ji = JFS_IP(inode);
  	struct super_block *sb = inode->i_sb;
  	int size;
  	int ea_size = sizeDXD(&ji->ea);
  	int blocks_needed, current_blocks;
  	s64 blkno;
  	int rc;
  	int quota_allocation = 0;
  
  	/* When fsck.jfs clears a bad ea, it doesn't clear the size */
  	if (ji->ea.flag == 0)
  		ea_size = 0;
  
  	if (ea_size == 0) {
  		if (min_size == 0) {
  			ea_buf->flag = 0;
  			ea_buf->max_size = 0;
  			ea_buf->xattr = NULL;
  			return 0;
  		}
  		if ((min_size <= sizeof (ji->i_inline_ea)) &&
  		    (ji->mode2 & INLINEEA)) {
  			ea_buf->flag = EA_INLINE | EA_NEW;
  			ea_buf->max_size = sizeof (ji->i_inline_ea);
  			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
  			DXDlength(&ea_buf->new_ea, 0);
  			DXDaddress(&ea_buf->new_ea, 0);
  			ea_buf->new_ea.flag = DXD_INLINE;
  			DXDsize(&ea_buf->new_ea, min_size);
  			return 0;
  		}
  		current_blocks = 0;
  	} else if (ji->ea.flag & DXD_INLINE) {
  		if (min_size <= sizeof (ji->i_inline_ea)) {
  			ea_buf->flag = EA_INLINE;
  			ea_buf->max_size = sizeof (ji->i_inline_ea);
  			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
  			goto size_check;
  		}
  		current_blocks = 0;
  	} else {
  		if (!(ji->ea.flag & DXD_EXTENT)) {
eb8630d7d   Joe Perches   jfs: Update jfs_e...
465
466
  			jfs_error(sb, "invalid ea.flag
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
473
474
475
476
  			return -EIO;
  		}
  		current_blocks = (ea_size + sb->s_blocksize - 1) >>
  		    sb->s_blocksize_bits;
  	}
  	size = max(min_size, ea_size);
  
  	if (size > PSIZE) {
  		/*
  		 * To keep the rest of the code simple.  Allocate a
92d341341   Shankara Pailoor   jfs: Fix inconsis...
477
478
  		 * contiguous buffer to work with. Make the buffer large
  		 * enough to make use of the whole extent.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
  		 */
92d341341   Shankara Pailoor   jfs: Fix inconsis...
480
481
482
483
  		ea_buf->max_size = (size + sb->s_blocksize - 1) &
  		    ~(sb->s_blocksize - 1);
  
  		ea_buf->xattr = kmalloc(ea_buf->max_size, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
486
487
  		if (ea_buf->xattr == NULL)
  			return -ENOMEM;
  
  		ea_buf->flag = EA_MALLOC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
  
  		if (ea_size == 0)
  			return 0;
  
  		if ((rc = ea_read(inode, ea_buf->xattr))) {
  			kfree(ea_buf->xattr);
  			ea_buf->xattr = NULL;
  			return rc;
  		}
  		goto size_check;
  	}
  	blocks_needed = (min_size + sb->s_blocksize - 1) >>
  	    sb->s_blocksize_bits;
  
  	if (blocks_needed > current_blocks) {
  		/* Allocate new blocks to quota. */
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
504
505
  		rc = dquot_alloc_block(inode, blocks_needed);
  		if (rc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  			return -EDQUOT;
  
  		quota_allocation = blocks_needed;
  
  		rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
  			     &blkno);
  		if (rc)
  			goto clean_up;
  
  		DXDlength(&ea_buf->new_ea, blocks_needed);
  		DXDaddress(&ea_buf->new_ea, blkno);
  		ea_buf->new_ea.flag = DXD_EXTENT;
  		DXDsize(&ea_buf->new_ea, min_size);
  
  		ea_buf->flag = EA_EXTENT | EA_NEW;
  
  		ea_buf->mp = get_metapage(inode, blkno,
  					  blocks_needed << sb->s_blocksize_bits,
  					  1);
  		if (ea_buf->mp == NULL) {
  			dbFree(inode, blkno, (s64) blocks_needed);
  			rc = -EIO;
  			goto clean_up;
  		}
  		ea_buf->xattr = ea_buf->mp->data;
  		ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
  		    ~(sb->s_blocksize - 1);
  		if (ea_size == 0)
  			return 0;
  		if ((rc = ea_read(inode, ea_buf->xattr))) {
  			discard_metapage(ea_buf->mp);
  			dbFree(inode, blkno, (s64) blocks_needed);
  			goto clean_up;
  		}
  		goto size_check;
  	}
  	ea_buf->flag = EA_EXTENT;
  	ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
  				   lengthDXD(&ji->ea) << sb->s_blocksize_bits,
  				   1);
  	if (ea_buf->mp == NULL) {
  		rc = -EIO;
  		goto clean_up;
  	}
  	ea_buf->xattr = ea_buf->mp->data;
  	ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
  	    ~(sb->s_blocksize - 1);
  
        size_check:
  	if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
  		printk(KERN_ERR "ea_get: invalid extended attribute
  ");
288e4d838   Dave Kleikamp   JFS: Update print...
558
559
  		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
  				     ea_buf->xattr, ea_size, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
562
563
564
565
566
567
568
569
  		ea_release(inode, ea_buf);
  		rc = -EIO;
  		goto clean_up;
  	}
  
  	return ea_size;
  
        clean_up:
  	/* Rollback quota allocation */
  	if (quota_allocation)
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
570
  		dquot_free_block(inode, quota_allocation);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  
  	return (rc);
  }
  
  static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
  {
  	if (ea_buf->flag & EA_MALLOC)
  		kfree(ea_buf->xattr);
  	else if (ea_buf->flag & EA_EXTENT) {
  		assert(ea_buf->mp);
  		release_metapage(ea_buf->mp);
  
  		if (ea_buf->flag & EA_NEW)
  			dbFree(inode, addressDXD(&ea_buf->new_ea),
  			       lengthDXD(&ea_buf->new_ea));
  	}
  }
4f4b401bf   Dave Kleikamp   JFS: allow extend...
588
589
  static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
  		  int new_size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
591
592
593
  {
  	struct jfs_inode_info *ji = JFS_IP(inode);
  	unsigned long old_blocks, new_blocks;
  	int rc = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
  
  	if (new_size == 0) {
  		ea_release(inode, ea_buf);
  		ea_buf = NULL;
  	} else if (ea_buf->flag & EA_INLINE) {
  		assert(new_size <= sizeof (ji->i_inline_ea));
  		ji->mode2 &= ~INLINEEA;
  		ea_buf->new_ea.flag = DXD_INLINE;
  		DXDsize(&ea_buf->new_ea, new_size);
  		DXDaddress(&ea_buf->new_ea, 0);
  		DXDlength(&ea_buf->new_ea, 0);
  	} else if (ea_buf->flag & EA_MALLOC) {
  		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  		kfree(ea_buf->xattr);
  	} else if (ea_buf->flag & EA_NEW) {
  		/* We have already allocated a new dxd */
  		flush_metapage(ea_buf->mp);
  	} else {
  		/* ->xattr must point to original ea's metapage */
  		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  		discard_metapage(ea_buf->mp);
  	}
  	if (rc)
  		return rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  	old_blocks = new_blocks = 0;
  
  	if (ji->ea.flag & DXD_EXTENT) {
  		invalidate_dxd_metapages(inode, ji->ea);
  		old_blocks = lengthDXD(&ji->ea);
  	}
  
  	if (ea_buf) {
  		txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
  		if (ea_buf->new_ea.flag & DXD_EXTENT) {
  			new_blocks = lengthDXD(&ea_buf->new_ea);
  			if (ji->ea.flag & DXD_INLINE)
  				ji->mode2 |= INLINEEA;
  		}
  		ji->ea = ea_buf->new_ea;
  	} else {
  		txEA(tid, inode, &ji->ea, NULL);
  		if (ji->ea.flag & DXD_INLINE)
  			ji->mode2 |= INLINEEA;
  		ji->ea.flag = 0;
  		ji->ea.size = 0;
  	}
  
  	/* If old blocks exist, they must be removed from quota allocation. */
  	if (old_blocks)
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
643
  		dquot_free_block(inode, old_blocks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644

078cd8279   Deepa Dinamani   fs: Replace CURRE...
645
  	inode->i_ctime = current_time(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646

4f4b401bf   Dave Kleikamp   JFS: allow extend...
647
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
  }
4f4b401bf   Dave Kleikamp   JFS: allow extend...
649
650
  int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
  		   const void *value, size_t value_len, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
652
653
654
655
656
657
658
  {
  	struct jfs_ea_list *ealist;
  	struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
  	struct ea_buffer ea_buf;
  	int old_ea_size = 0;
  	int xattr_size;
  	int new_size;
  	int namelen = strlen(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
660
661
  	int found = 0;
  	int rc;
  	int length;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
  	down_write(&JFS_IP(inode)->xattr_sem);
  
  	xattr_size = ea_get(inode, &ea_buf, 0);
  	if (xattr_size < 0) {
  		rc = xattr_size;
  		goto out;
  	}
  
        again:
  	ealist = (struct jfs_ea_list *) ea_buf.xattr;
  	new_size = sizeof (struct jfs_ea_list);
  
  	if (xattr_size) {
  		for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
  		     ea = NEXT_EA(ea)) {
  			if ((namelen == ea->namelen) &&
  			    (memcmp(name, ea->name, namelen) == 0)) {
  				found = 1;
  				if (flags & XATTR_CREATE) {
  					rc = -EEXIST;
  					goto release;
  				}
  				old_ea = ea;
  				old_ea_size = EA_SIZE(ea);
  				next_ea = NEXT_EA(ea);
  			} else
  				new_size += EA_SIZE(ea);
  		}
  	}
  
  	if (!found) {
  		if (flags & XATTR_REPLACE) {
  			rc = -ENODATA;
  			goto release;
  		}
  		if (value == NULL) {
  			rc = 0;
  			goto release;
  		}
  	}
  	if (value)
  		new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
  
  	if (new_size > ea_buf.max_size) {
  		/*
  		 * We need to allocate more space for merged ea list.
  		 * We should only have loop to again: once.
  		 */
  		ea_release(inode, &ea_buf);
  		xattr_size = ea_get(inode, &ea_buf, new_size);
  		if (xattr_size < 0) {
  			rc = xattr_size;
  			goto out;
  		}
  		goto again;
  	}
  
  	/* Remove old ea of the same name */
  	if (found) {
  		/* number of bytes following target EA */
  		length = (char *) END_EALIST(ealist) - (char *) next_ea;
  		if (length > 0)
  			memmove(old_ea, next_ea, length);
  		xattr_size -= old_ea_size;
  	}
  
  	/* Add new entry to the end */
  	if (value) {
  		if (xattr_size == 0)
  			/* Completely new ea list */
  			xattr_size = sizeof (struct jfs_ea_list);
0439e091e   Jie Liu   jfs: fix xattr va...
733
734
735
736
737
738
739
740
741
742
743
744
  		/*
  		 * The size of EA value is limitted by on-disk format up to
  		 *  __le16, there would be an overflow if the size is equal
  		 * to XATTR_SIZE_MAX (65536).  In order to avoid this issue,
  		 * we can pre-checkup the value size against USHRT_MAX, and
  		 * return -E2BIG in this case, which is consistent with the
  		 * VFS setxattr interface.
  		 */
  		if (value_len >= USHRT_MAX) {
  			rc = -E2BIG;
  			goto release;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
747
748
749
750
751
752
753
754
755
756
757
758
  		ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
  		ea->flag = 0;
  		ea->namelen = namelen;
  		ea->valuelen = (cpu_to_le16(value_len));
  		memcpy(ea->name, name, namelen);
  		ea->name[namelen] = 0;
  		if (value_len)
  			memcpy(&ea->name[namelen + 1], value, value_len);
  		xattr_size += EA_SIZE(ea);
  	}
  
  	/* DEBUG - If we did this right, these number match */
  	if (xattr_size != new_size) {
  		printk(KERN_ERR
0439e091e   Jie Liu   jfs: fix xattr va...
759
760
  		       "__jfs_setxattr: xattr_size = %d, new_size = %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
762
763
764
765
766
767
768
769
770
771
772
773
  		       xattr_size, new_size);
  
  		rc = -EINVAL;
  		goto release;
  	}
  
  	/*
  	 * If we're left with an empty list, there's no ea
  	 */
  	if (new_size == sizeof (struct jfs_ea_list))
  		new_size = 0;
  
  	ealist->size = cpu_to_le32(new_size);
4f4b401bf   Dave Kleikamp   JFS: allow extend...
774
  	rc = ea_put(tid, inode, &ea_buf, new_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
776
777
778
779
780
  
  	goto out;
        release:
  	ea_release(inode, &ea_buf);
        out:
  	up_write(&JFS_IP(inode)->xattr_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
782
  	return rc;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
783
784
785
786
787
788
789
790
791
  ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
  		       size_t buf_size)
  {
  	struct jfs_ea_list *ealist;
  	struct jfs_ea *ea;
  	struct ea_buffer ea_buf;
  	int xattr_size;
  	ssize_t size;
  	int namelen = strlen(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
  	char *value;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
  	down_read(&JFS_IP(inode)->xattr_sem);
  
  	xattr_size = ea_get(inode, &ea_buf, 0);
  
  	if (xattr_size < 0) {
  		size = xattr_size;
  		goto out;
  	}
  
  	if (xattr_size == 0)
  		goto not_found;
  
  	ealist = (struct jfs_ea_list *) ea_buf.xattr;
  
  	/* Find the named attribute */
  	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
  		if ((namelen == ea->namelen) &&
  		    memcmp(name, ea->name, namelen) == 0) {
  			/* Found it */
  			size = le16_to_cpu(ea->valuelen);
  			if (!data)
  				goto release;
  			else if (size > buf_size) {
  				size = -ERANGE;
  				goto release;
  			}
  			value = ((char *) &ea->name) + ea->namelen + 1;
  			memcpy(data, value, size);
  			goto release;
  		}
        not_found:
  	size = -ENODATA;
        release:
  	ea_release(inode, &ea_buf);
        out:
  	up_read(&JFS_IP(inode)->xattr_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
829
830
  	return size;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
831
832
833
834
835
836
837
838
839
840
841
842
  /*
   * No special permissions are needed to list attributes except for trusted.*
   */
  static inline int can_list(struct jfs_ea *ea)
  {
  	return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
  			    XATTR_TRUSTED_PREFIX_LEN) ||
  		capable(CAP_SYS_ADMIN));
  }
  
  ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
  {
2b0143b5c   David Howells   VFS: normal files...
843
  	struct inode *inode = d_inode(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  	char *buffer;
  	ssize_t size = 0;
  	int xattr_size;
  	struct jfs_ea_list *ealist;
  	struct jfs_ea *ea;
  	struct ea_buffer ea_buf;
  
  	down_read(&JFS_IP(inode)->xattr_sem);
  
  	xattr_size = ea_get(inode, &ea_buf, 0);
  	if (xattr_size < 0) {
  		size = xattr_size;
  		goto out;
  	}
  
  	if (xattr_size == 0)
  		goto release;
  
  	ealist = (struct jfs_ea_list *) ea_buf.xattr;
  
  	/* compute required size of list */
  	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
63f83c9fc   Dave Kleikamp   JFS: White space ...
866
  		if (can_list(ea))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
868
869
870
871
872
873
874
875
876
877
878
879
880
  			size += name_size(ea) + 1;
  	}
  
  	if (!data)
  		goto release;
  
  	if (size > buf_size) {
  		size = -ERANGE;
  		goto release;
  	}
  
  	/* Copy attribute names to buffer */
  	buffer = data;
  	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
63f83c9fc   Dave Kleikamp   JFS: White space ...
881
  		if (can_list(ea)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
883
884
885
886
887
888
889
890
891
892
  			int namelen = copy_name(buffer, ea);
  			buffer += namelen + 1;
  		}
  	}
  
        release:
  	ea_release(inode, &ea_buf);
        out:
  	up_read(&JFS_IP(inode)->xattr_sem);
  	return size;
  }
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
893
894
  static int __jfs_xattr_set(struct inode *inode, const char *name,
  			   const void *value, size_t size, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895
  {
4f4b401bf   Dave Kleikamp   JFS: allow extend...
896
  	struct jfs_inode_info *ji = JFS_IP(inode);
4f4b401bf   Dave Kleikamp   JFS: allow extend...
897
  	tid_t tid;
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
898
  	int rc;
c18f7b512   Dave Kleikamp   jfs: fix generic ...
899

4f4b401bf   Dave Kleikamp   JFS: allow extend...
900
  	tid = txBegin(inode->i_sb, 0);
1de87444f   Ingo Molnar   JFS: semaphore to...
901
  	mutex_lock(&ji->commit_mutex);
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
902
  	rc = __jfs_setxattr(tid, inode, name, value, size, flags);
4f4b401bf   Dave Kleikamp   JFS: allow extend...
903
904
905
  	if (!rc)
  		rc = txCommit(tid, 1, &inode, 0);
  	txEnd(tid);
1de87444f   Ingo Molnar   JFS: semaphore to...
906
  	mutex_unlock(&ji->commit_mutex);
4f4b401bf   Dave Kleikamp   JFS: allow extend...
907
908
  
  	return rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
909
  }
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
910

c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
911
912
913
914
915
916
917
918
919
  static int jfs_xattr_get(const struct xattr_handler *handler,
  			 struct dentry *unused, struct inode *inode,
  			 const char *name, void *value, size_t size)
  {
  	name = xattr_full_name(handler, name);
  	return __jfs_getxattr(inode, name, value, size);
  }
  
  static int jfs_xattr_set(const struct xattr_handler *handler,
593012268   Al Viro   switch xattr_hand...
920
921
922
  			 struct dentry *unused, struct inode *inode,
  			 const char *name, const void *value,
  			 size_t size, int flags)
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
923
  {
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
924
925
926
927
928
929
930
931
932
933
934
935
936
937
  	name = xattr_full_name(handler, name);
  	return __jfs_xattr_set(inode, name, value, size, flags);
  }
  
  static int jfs_xattr_get_os2(const struct xattr_handler *handler,
  			     struct dentry *unused, struct inode *inode,
  			     const char *name, void *value, size_t size)
  {
  	if (is_known_namespace(name))
  		return -EOPNOTSUPP;
  	return __jfs_getxattr(inode, name, value, size);
  }
  
  static int jfs_xattr_set_os2(const struct xattr_handler *handler,
593012268   Al Viro   switch xattr_hand...
938
939
940
  			     struct dentry *unused, struct inode *inode,
  			     const char *name, const void *value,
  			     size_t size, int flags)
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
941
  {
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
  	if (is_known_namespace(name))
  		return -EOPNOTSUPP;
  	return __jfs_xattr_set(inode, name, value, size, flags);
  }
  
  static const struct xattr_handler jfs_user_xattr_handler = {
  	.prefix = XATTR_USER_PREFIX,
  	.get = jfs_xattr_get,
  	.set = jfs_xattr_set,
  };
  
  static const struct xattr_handler jfs_os2_xattr_handler = {
  	.prefix = XATTR_OS2_PREFIX,
  	.get = jfs_xattr_get_os2,
  	.set = jfs_xattr_set_os2,
  };
  
  static const struct xattr_handler jfs_security_xattr_handler = {
  	.prefix = XATTR_SECURITY_PREFIX,
  	.get = jfs_xattr_get,
  	.set = jfs_xattr_set,
  };
  
  static const struct xattr_handler jfs_trusted_xattr_handler = {
  	.prefix = XATTR_TRUSTED_PREFIX,
  	.get = jfs_xattr_get,
  	.set = jfs_xattr_set,
  };
2cc6a5a01   Christoph Hellwig   jfs: use generic ...
970
  const struct xattr_handler *jfs_xattr_handlers[] = {
c18f7b512   Dave Kleikamp   jfs: fix generic ...
971
  #ifdef CONFIG_JFS_POSIX_ACL
2cc6a5a01   Christoph Hellwig   jfs: use generic ...
972
973
974
  	&posix_acl_access_xattr_handler,
  	&posix_acl_default_xattr_handler,
  #endif
c8b6056a5   Andreas Gruenbacher   jfs: Switch to ge...
975
976
977
978
  	&jfs_os2_xattr_handler,
  	&jfs_user_xattr_handler,
  	&jfs_security_xattr_handler,
  	&jfs_trusted_xattr_handler,
2cc6a5a01   Christoph Hellwig   jfs: use generic ...
979
980
  	NULL,
  };
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
981
  #ifdef CONFIG_JFS_SECURITY
21d1101f0   Dave Kleikamp   jfs: fix sparse w...
982
983
  static int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  			  void *fs_info)
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
984
  {
9d8f13ba3   Mimi Zohar   security: new sec...
985
986
  	const struct xattr *xattr;
  	tid_t *tid = fs_info;
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
987
  	char *name;
9d8f13ba3   Mimi Zohar   security: new sec...
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  	int err = 0;
  
  	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  			       strlen(xattr->name) + 1, GFP_NOFS);
  		if (!name) {
  			err = -ENOMEM;
  			break;
  		}
  		strcpy(name, XATTR_SECURITY_PREFIX);
  		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  
  		err = __jfs_setxattr(*tid, inode, name,
  				     xattr->value, xattr->value_len, 0);
  		kfree(name);
  		if (err < 0)
  			break;
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
1005
  	}
9d8f13ba3   Mimi Zohar   security: new sec...
1006
1007
  	return err;
  }
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
1008

9d8f13ba3   Mimi Zohar   security: new sec...
1009
1010
1011
1012
1013
  int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
  		      const struct qstr *qstr)
  {
  	return security_inode_init_security(inode, dir, qstr,
  					    &jfs_initxattrs, &tid);
1d15b10f9   Dave Kleikamp   JFS: Implement jf...
1014
1015
  }
  #endif