Blame view

fs/xfs/xfs_attr_list.c 15.5 KB
abec5f2bf   Dave Chinner   xfs: split out at...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /*
   * Copyright (c) 2000-2005 Silicon Graphics, Inc.
   * Copyright (c) 2013 Red Hat, Inc.
   * All Rights Reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it would be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write the Free Software Foundation,
   * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   */
  #include "xfs.h"
  #include "xfs_fs.h"
a4fbe6ab1   Dave Chinner   xfs: decouple ino...
21
  #include "xfs_format.h"
239880ef6   Dave Chinner   xfs: decouple log...
22
23
  #include "xfs_log_format.h"
  #include "xfs_trans_resv.h"
abec5f2bf   Dave Chinner   xfs: split out at...
24
  #include "xfs_bit.h"
abec5f2bf   Dave Chinner   xfs: split out at...
25
  #include "xfs_mount.h"
570627875   Dave Chinner   xfs: unify direct...
26
  #include "xfs_da_format.h"
abec5f2bf   Dave Chinner   xfs: split out at...
27
  #include "xfs_da_btree.h"
abec5f2bf   Dave Chinner   xfs: split out at...
28
  #include "xfs_inode.h"
239880ef6   Dave Chinner   xfs: decouple log...
29
  #include "xfs_trans.h"
abec5f2bf   Dave Chinner   xfs: split out at...
30
31
32
  #include "xfs_inode_item.h"
  #include "xfs_bmap.h"
  #include "xfs_attr.h"
a4fbe6ab1   Dave Chinner   xfs: decouple ino...
33
34
  #include "xfs_attr_sf.h"
  #include "xfs_attr_remote.h"
abec5f2bf   Dave Chinner   xfs: split out at...
35
36
37
38
39
  #include "xfs_attr_leaf.h"
  #include "xfs_error.h"
  #include "xfs_trace.h"
  #include "xfs_buf_item.h"
  #include "xfs_cksum.h"
4bceb18f1   Dave Chinner   xfs: vectorise DA...
40
  #include "xfs_dir2.h"
abec5f2bf   Dave Chinner   xfs: split out at...
41
42
43
44
45
46
47
48
49
  
  STATIC int
  xfs_attr_shortform_compare(const void *a, const void *b)
  {
  	xfs_attr_sf_sort_t *sa, *sb;
  
  	sa = (xfs_attr_sf_sort_t *)a;
  	sb = (xfs_attr_sf_sort_t *)b;
  	if (sa->hash < sb->hash) {
d99831ff3   Eric Sandeen   xfs: return is no...
50
  		return -1;
abec5f2bf   Dave Chinner   xfs: split out at...
51
  	} else if (sa->hash > sb->hash) {
d99831ff3   Eric Sandeen   xfs: return is no...
52
  		return 1;
abec5f2bf   Dave Chinner   xfs: split out at...
53
  	} else {
d99831ff3   Eric Sandeen   xfs: return is no...
54
  		return sa->entno - sb->entno;
abec5f2bf   Dave Chinner   xfs: split out at...
55
56
57
58
59
60
61
62
63
64
65
66
67
  	}
  }
  
  #define XFS_ISRESET_CURSOR(cursor) \
  	(!((cursor)->initted) && !((cursor)->hashval) && \
  	 !((cursor)->blkno) && !((cursor)->offset))
  /*
   * Copy out entries of shortform attribute lists for attr_list().
   * Shortform attribute lists are not stored in hashval sorted order.
   * If the output buffer is not large enough to hold them all, then we
   * we have to calculate each entries' hashvalue and sort them before
   * we can begin returning them to the user.
   */
0d5a75e9e   Eric Sandeen   xfs: make several...
68
  static int
abec5f2bf   Dave Chinner   xfs: split out at...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  xfs_attr_shortform_list(xfs_attr_list_context_t *context)
  {
  	attrlist_cursor_kern_t *cursor;
  	xfs_attr_sf_sort_t *sbuf, *sbp;
  	xfs_attr_shortform_t *sf;
  	xfs_attr_sf_entry_t *sfe;
  	xfs_inode_t *dp;
  	int sbsize, nsbuf, count, i;
  	int error;
  
  	ASSERT(context != NULL);
  	dp = context->dp;
  	ASSERT(dp != NULL);
  	ASSERT(dp->i_afp != NULL);
  	sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
  	ASSERT(sf != NULL);
  	if (!sf->hdr.count)
d99831ff3   Eric Sandeen   xfs: return is no...
86
  		return 0;
abec5f2bf   Dave Chinner   xfs: split out at...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  	cursor = context->cursor;
  	ASSERT(cursor != NULL);
  
  	trace_xfs_attr_list_sf(context);
  
  	/*
  	 * If the buffer is large enough and the cursor is at the start,
  	 * do not bother with sorting since we will return everything in
  	 * one buffer and another call using the cursor won't need to be
  	 * made.
  	 * Note the generous fudge factor of 16 overhead bytes per entry.
  	 * If bufsize is zero then put_listent must be a search function
  	 * and can just scan through what we have.
  	 */
  	if (context->bufsize == 0 ||
  	    (XFS_ISRESET_CURSOR(cursor) &&
               (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
  		for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
  			error = context->put_listent(context,
  					   sfe->flags,
  					   sfe->nameval,
  					   (int)sfe->namelen,
e5bd12bfe   Eric Sandeen   xfs: don't pass v...
109
  					   (int)sfe->valuelen);
2a6fba6d2   Eric Sandeen   xfs: only return ...
110
111
  			if (error)
  				return error;
abec5f2bf   Dave Chinner   xfs: split out at...
112
113
114
115
116
117
  			/*
  			 * Either search callback finished early or
  			 * didn't fit it all in the buffer after all.
  			 */
  			if (context->seen_enough)
  				break;
abec5f2bf   Dave Chinner   xfs: split out at...
118
119
120
  			sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
  		}
  		trace_xfs_attr_list_sf_all(context);
d99831ff3   Eric Sandeen   xfs: return is no...
121
  		return 0;
abec5f2bf   Dave Chinner   xfs: split out at...
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
  	}
  
  	/* do no more for a search callback */
  	if (context->bufsize == 0)
  		return 0;
  
  	/*
  	 * It didn't all fit, so we have to sort everything on hashval.
  	 */
  	sbsize = sf->hdr.count * sizeof(*sbuf);
  	sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
  
  	/*
  	 * Scan the attribute list for the rest of the entries, storing
  	 * the relevant info from only those that match into a buffer.
  	 */
  	nsbuf = 0;
  	for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
  		if (unlikely(
  		    ((char *)sfe < (char *)sf) ||
  		    ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
  			XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
  					     XFS_ERRLEVEL_LOW,
  					     context->dp->i_mount, sfe);
  			kmem_free(sbuf);
2451337dd   Dave Chinner   xfs: global error...
147
  			return -EFSCORRUPTED;
abec5f2bf   Dave Chinner   xfs: split out at...
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
179
180
181
182
183
184
  		}
  
  		sbp->entno = i;
  		sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
  		sbp->name = sfe->nameval;
  		sbp->namelen = sfe->namelen;
  		/* These are bytes, and both on-disk, don't endian-flip */
  		sbp->valuelen = sfe->valuelen;
  		sbp->flags = sfe->flags;
  		sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
  		sbp++;
  		nsbuf++;
  	}
  
  	/*
  	 * Sort the entries on hash then entno.
  	 */
  	xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
  
  	/*
  	 * Re-find our place IN THE SORTED LIST.
  	 */
  	count = 0;
  	cursor->initted = 1;
  	cursor->blkno = 0;
  	for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
  		if (sbp->hash == cursor->hashval) {
  			if (cursor->offset == count) {
  				break;
  			}
  			count++;
  		} else if (sbp->hash > cursor->hashval) {
  			break;
  		}
  	}
  	if (i == nsbuf) {
  		kmem_free(sbuf);
d99831ff3   Eric Sandeen   xfs: return is no...
185
  		return 0;
abec5f2bf   Dave Chinner   xfs: split out at...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  	}
  
  	/*
  	 * Loop putting entries into the user buffer.
  	 */
  	for ( ; i < nsbuf; i++, sbp++) {
  		if (cursor->hashval != sbp->hash) {
  			cursor->hashval = sbp->hash;
  			cursor->offset = 0;
  		}
  		error = context->put_listent(context,
  					sbp->flags,
  					sbp->name,
  					sbp->namelen,
e5bd12bfe   Eric Sandeen   xfs: don't pass v...
200
  					sbp->valuelen);
2e83b79b2   Mateusz Guzik   xfs: fix two memo...
201
202
  		if (error) {
  			kmem_free(sbuf);
abec5f2bf   Dave Chinner   xfs: split out at...
203
  			return error;
2e83b79b2   Mateusz Guzik   xfs: fix two memo...
204
  		}
abec5f2bf   Dave Chinner   xfs: split out at...
205
206
207
208
209
210
  		if (context->seen_enough)
  			break;
  		cursor->offset++;
  	}
  
  	kmem_free(sbuf);
d99831ff3   Eric Sandeen   xfs: return is no...
211
  	return 0;
abec5f2bf   Dave Chinner   xfs: split out at...
212
213
214
215
216
217
218
219
220
221
222
223
224
  }
  
  STATIC int
  xfs_attr_node_list(xfs_attr_list_context_t *context)
  {
  	attrlist_cursor_kern_t *cursor;
  	xfs_attr_leafblock_t *leaf;
  	xfs_da_intnode_t *node;
  	struct xfs_attr3_icleaf_hdr leafhdr;
  	struct xfs_da3_icnode_hdr nodehdr;
  	struct xfs_da_node_entry *btree;
  	int error, i;
  	struct xfs_buf *bp;
4bceb18f1   Dave Chinner   xfs: vectorise DA...
225
  	struct xfs_inode	*dp = context->dp;
2f6612415   Brian Foster   xfs: pass attr ge...
226
  	struct xfs_mount	*mp = dp->i_mount;
abec5f2bf   Dave Chinner   xfs: split out at...
227
228
229
230
231
232
233
234
235
236
237
238
239
  
  	trace_xfs_attr_node_list(context);
  
  	cursor = context->cursor;
  	cursor->initted = 1;
  
  	/*
  	 * Do all sorts of validation on the passed-in cursor structure.
  	 * If anything is amiss, ignore the cursor and look up the hashval
  	 * starting from the btree root.
  	 */
  	bp = NULL;
  	if (cursor->blkno > 0) {
4bceb18f1   Dave Chinner   xfs: vectorise DA...
240
  		error = xfs_da3_node_read(NULL, dp, cursor->blkno, -1,
abec5f2bf   Dave Chinner   xfs: split out at...
241
  					      &bp, XFS_ATTR_FORK);
2451337dd   Dave Chinner   xfs: global error...
242
  		if ((error != 0) && (error != -EFSCORRUPTED))
d99831ff3   Eric Sandeen   xfs: return is no...
243
  			return error;
abec5f2bf   Dave Chinner   xfs: split out at...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  		if (bp) {
  			struct xfs_attr_leaf_entry *entries;
  
  			node = bp->b_addr;
  			switch (be16_to_cpu(node->hdr.info.magic)) {
  			case XFS_DA_NODE_MAGIC:
  			case XFS_DA3_NODE_MAGIC:
  				trace_xfs_attr_list_wrong_blk(context);
  				xfs_trans_brelse(NULL, bp);
  				bp = NULL;
  				break;
  			case XFS_ATTR_LEAF_MAGIC:
  			case XFS_ATTR3_LEAF_MAGIC:
  				leaf = bp->b_addr;
2f6612415   Brian Foster   xfs: pass attr ge...
258
259
  				xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo,
  							     &leafhdr, leaf);
abec5f2bf   Dave Chinner   xfs: split out at...
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
  				entries = xfs_attr3_leaf_entryp(leaf);
  				if (cursor->hashval > be32_to_cpu(
  						entries[leafhdr.count - 1].hashval)) {
  					trace_xfs_attr_list_wrong_blk(context);
  					xfs_trans_brelse(NULL, bp);
  					bp = NULL;
  				} else if (cursor->hashval <= be32_to_cpu(
  						entries[0].hashval)) {
  					trace_xfs_attr_list_wrong_blk(context);
  					xfs_trans_brelse(NULL, bp);
  					bp = NULL;
  				}
  				break;
  			default:
  				trace_xfs_attr_list_wrong_blk(context);
  				xfs_trans_brelse(NULL, bp);
  				bp = NULL;
  			}
  		}
  	}
  
  	/*
  	 * We did not find what we expected given the cursor's contents,
  	 * so we start from the top and work down based on the hash value.
  	 * Note that start of node block is same as start of leaf block.
  	 */
  	if (bp == NULL) {
  		cursor->blkno = 0;
  		for (;;) {
  			__uint16_t magic;
4bceb18f1   Dave Chinner   xfs: vectorise DA...
290
  			error = xfs_da3_node_read(NULL, dp,
abec5f2bf   Dave Chinner   xfs: split out at...
291
292
293
  						      cursor->blkno, -1, &bp,
  						      XFS_ATTR_FORK);
  			if (error)
d99831ff3   Eric Sandeen   xfs: return is no...
294
  				return error;
abec5f2bf   Dave Chinner   xfs: split out at...
295
296
297
298
299
300
301
302
303
304
305
306
  			node = bp->b_addr;
  			magic = be16_to_cpu(node->hdr.info.magic);
  			if (magic == XFS_ATTR_LEAF_MAGIC ||
  			    magic == XFS_ATTR3_LEAF_MAGIC)
  				break;
  			if (magic != XFS_DA_NODE_MAGIC &&
  			    magic != XFS_DA3_NODE_MAGIC) {
  				XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
  						     XFS_ERRLEVEL_LOW,
  						     context->dp->i_mount,
  						     node);
  				xfs_trans_brelse(NULL, bp);
2451337dd   Dave Chinner   xfs: global error...
307
  				return -EFSCORRUPTED;
abec5f2bf   Dave Chinner   xfs: split out at...
308
  			}
01ba43b87   Dave Chinner   xfs: vectorise en...
309
  			dp->d_ops->node_hdr_from_disk(&nodehdr, node);
4bceb18f1   Dave Chinner   xfs: vectorise DA...
310
  			btree = dp->d_ops->node_tree_p(node);
abec5f2bf   Dave Chinner   xfs: split out at...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  			for (i = 0; i < nodehdr.count; btree++, i++) {
  				if (cursor->hashval
  						<= be32_to_cpu(btree->hashval)) {
  					cursor->blkno = be32_to_cpu(btree->before);
  					trace_xfs_attr_list_node_descend(context,
  									 btree);
  					break;
  				}
  			}
  			if (i == nodehdr.count) {
  				xfs_trans_brelse(NULL, bp);
  				return 0;
  			}
  			xfs_trans_brelse(NULL, bp);
  		}
  	}
  	ASSERT(bp != NULL);
  
  	/*
  	 * Roll upward through the blocks, processing each leaf block in
  	 * order.  As long as there is space in the result buffer, keep
  	 * adding the information.
  	 */
  	for (;;) {
  		leaf = bp->b_addr;
  		error = xfs_attr3_leaf_list_int(bp, context);
  		if (error) {
  			xfs_trans_brelse(NULL, bp);
  			return error;
  		}
2f6612415   Brian Foster   xfs: pass attr ge...
341
  		xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
abec5f2bf   Dave Chinner   xfs: split out at...
342
343
344
345
  		if (context->seen_enough || leafhdr.forw == 0)
  			break;
  		cursor->blkno = leafhdr.forw;
  		xfs_trans_brelse(NULL, bp);
4bceb18f1   Dave Chinner   xfs: vectorise DA...
346
  		error = xfs_attr3_leaf_read(NULL, dp, cursor->blkno, -1, &bp);
abec5f2bf   Dave Chinner   xfs: split out at...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
  		if (error)
  			return error;
  	}
  	xfs_trans_brelse(NULL, bp);
  	return 0;
  }
  
  /*
   * Copy out attribute list entries for attr_list(), for leaf attribute lists.
   */
  int
  xfs_attr3_leaf_list_int(
  	struct xfs_buf			*bp,
  	struct xfs_attr_list_context	*context)
  {
  	struct attrlist_cursor_kern	*cursor;
  	struct xfs_attr_leafblock	*leaf;
  	struct xfs_attr3_icleaf_hdr	ichdr;
  	struct xfs_attr_leaf_entry	*entries;
  	struct xfs_attr_leaf_entry	*entry;
  	int				retval;
  	int				i;
2f6612415   Brian Foster   xfs: pass attr ge...
369
  	struct xfs_mount		*mp = context->dp->i_mount;
abec5f2bf   Dave Chinner   xfs: split out at...
370
371
372
373
  
  	trace_xfs_attr_list_leaf(context);
  
  	leaf = bp->b_addr;
2f6612415   Brian Foster   xfs: pass attr ge...
374
  	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
abec5f2bf   Dave Chinner   xfs: split out at...
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
406
407
408
409
410
411
412
  	entries = xfs_attr3_leaf_entryp(leaf);
  
  	cursor = context->cursor;
  	cursor->initted = 1;
  
  	/*
  	 * Re-find our place in the leaf block if this is a new syscall.
  	 */
  	if (context->resynch) {
  		entry = &entries[0];
  		for (i = 0; i < ichdr.count; entry++, i++) {
  			if (be32_to_cpu(entry->hashval) == cursor->hashval) {
  				if (cursor->offset == context->dupcnt) {
  					context->dupcnt = 0;
  					break;
  				}
  				context->dupcnt++;
  			} else if (be32_to_cpu(entry->hashval) >
  					cursor->hashval) {
  				context->dupcnt = 0;
  				break;
  			}
  		}
  		if (i == ichdr.count) {
  			trace_xfs_attr_list_notfound(context);
  			return 0;
  		}
  	} else {
  		entry = &entries[0];
  		i = 0;
  	}
  	context->resynch = 0;
  
  	/*
  	 * We have found our place, start copying out the new attributes.
  	 */
  	retval = 0;
  	for (; i < ichdr.count; entry++, i++) {
3ab3ffcac   Eric Sandeen   xfs: collapse cas...
413
414
  		char *name;
  		int namelen, valuelen;
abec5f2bf   Dave Chinner   xfs: split out at...
415
416
417
418
419
420
421
422
423
  		if (be32_to_cpu(entry->hashval) != cursor->hashval) {
  			cursor->hashval = be32_to_cpu(entry->hashval);
  			cursor->offset = 0;
  		}
  
  		if (entry->flags & XFS_ATTR_INCOMPLETE)
  			continue;		/* skip incomplete entries */
  
  		if (entry->flags & XFS_ATTR_LOCAL) {
3ab3ffcac   Eric Sandeen   xfs: collapse cas...
424
  			xfs_attr_leaf_name_local_t *name_loc;
abec5f2bf   Dave Chinner   xfs: split out at...
425

3ab3ffcac   Eric Sandeen   xfs: collapse cas...
426
427
428
429
430
431
  			name_loc = xfs_attr3_leaf_name_local(leaf, i);
  			name = name_loc->nameval;
  			namelen = name_loc->namelen;
  			valuelen = be16_to_cpu(name_loc->valuelen);
  		} else {
  			xfs_attr_leaf_name_remote_t *name_rmt;
abec5f2bf   Dave Chinner   xfs: split out at...
432

3ab3ffcac   Eric Sandeen   xfs: collapse cas...
433
434
435
436
  			name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  			name = name_rmt->name;
  			namelen = name_rmt->namelen;
  			valuelen = be32_to_cpu(name_rmt->valuelen);
abec5f2bf   Dave Chinner   xfs: split out at...
437
  		}
3ab3ffcac   Eric Sandeen   xfs: collapse cas...
438
439
440
  
  		retval = context->put_listent(context, entry->flags,
  					      name, namelen, valuelen);
7af5ad28a   Eric Sandeen   xfs: remove put_v...
441
442
  		if (retval)
  			break;
abec5f2bf   Dave Chinner   xfs: split out at...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  		if (context->seen_enough)
  			break;
  		cursor->offset++;
  	}
  	trace_xfs_attr_list_leaf_end(context);
  	return retval;
  }
  
  /*
   * Copy out attribute entries for attr_list(), for leaf attribute lists.
   */
  STATIC int
  xfs_attr_leaf_list(xfs_attr_list_context_t *context)
  {
  	int error;
  	struct xfs_buf *bp;
  
  	trace_xfs_attr_leaf_list(context);
  
  	context->cursor->blkno = 0;
  	error = xfs_attr3_leaf_read(NULL, context->dp, 0, -1, &bp);
  	if (error)
b474c7ae4   Eric Sandeen   xfs: Nuke XFS_ERR...
465
  		return error;
abec5f2bf   Dave Chinner   xfs: split out at...
466
467
468
  
  	error = xfs_attr3_leaf_list_int(bp, context);
  	xfs_trans_brelse(NULL, bp);
b474c7ae4   Eric Sandeen   xfs: Nuke XFS_ERR...
469
  	return error;
abec5f2bf   Dave Chinner   xfs: split out at...
470
471
472
473
474
475
476
477
  }
  
  int
  xfs_attr_list_int(
  	xfs_attr_list_context_t *context)
  {
  	int error;
  	xfs_inode_t *dp = context->dp;
568d994e9   Christoph Hellwig   xfs: use xfs_iloc...
478
  	uint		lock_mode;
abec5f2bf   Dave Chinner   xfs: split out at...
479

ff6d6af23   Bill O'Donnell   xfs: per-filesyst...
480
  	XFS_STATS_INC(dp->i_mount, xs_attr_list);
abec5f2bf   Dave Chinner   xfs: split out at...
481
482
  
  	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
2451337dd   Dave Chinner   xfs: global error...
483
  		return -EIO;
abec5f2bf   Dave Chinner   xfs: split out at...
484

abec5f2bf   Dave Chinner   xfs: split out at...
485
486
487
  	/*
  	 * Decide on what work routines to call based on the inode size.
  	 */
568d994e9   Christoph Hellwig   xfs: use xfs_iloc...
488
  	lock_mode = xfs_ilock_attr_map_shared(dp);
abec5f2bf   Dave Chinner   xfs: split out at...
489
490
491
492
493
494
495
496
497
  	if (!xfs_inode_hasattr(dp)) {
  		error = 0;
  	} else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  		error = xfs_attr_shortform_list(context);
  	} else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  		error = xfs_attr_leaf_list(context);
  	} else {
  		error = xfs_attr_node_list(context);
  	}
568d994e9   Christoph Hellwig   xfs: use xfs_iloc...
498
  	xfs_iunlock(dp, lock_mode);
abec5f2bf   Dave Chinner   xfs: split out at...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
  	return error;
  }
  
  #define	ATTR_ENTBASESIZE		/* minimum bytes used by an attr */ \
  	(((struct attrlist_ent *) 0)->a_name - (char *) 0)
  #define	ATTR_ENTSIZE(namelen)		/* actual bytes used by an attr */ \
  	((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
  	 & ~(sizeof(u_int32_t)-1))
  
  /*
   * Format an attribute and copy it out to the user's buffer.
   * Take care to check values and protect against them changing later,
   * we may be reading them directly out of a user buffer.
   */
  STATIC int
  xfs_attr_put_listent(
  	xfs_attr_list_context_t *context,
  	int		flags,
  	unsigned char	*name,
  	int		namelen,
e5bd12bfe   Eric Sandeen   xfs: don't pass v...
519
  	int		valuelen)
abec5f2bf   Dave Chinner   xfs: split out at...
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
  {
  	struct attrlist *alist = (struct attrlist *)context->alist;
  	attrlist_ent_t *aep;
  	int arraytop;
  
  	ASSERT(!(context->flags & ATTR_KERNOVAL));
  	ASSERT(context->count >= 0);
  	ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
  	ASSERT(context->firstu >= sizeof(*alist));
  	ASSERT(context->firstu <= context->bufsize);
  
  	/*
  	 * Only list entries in the right namespace.
  	 */
  	if (((context->flags & ATTR_SECURE) == 0) !=
  	    ((flags & XFS_ATTR_SECURE) == 0))
  		return 0;
  	if (((context->flags & ATTR_ROOT) == 0) !=
  	    ((flags & XFS_ATTR_ROOT) == 0))
  		return 0;
  
  	arraytop = sizeof(*alist) +
  			context->count * sizeof(alist->al_offset[0]);
  	context->firstu -= ATTR_ENTSIZE(namelen);
  	if (context->firstu < arraytop) {
  		trace_xfs_attr_list_full(context);
  		alist->al_more = 1;
  		context->seen_enough = 1;
2a6fba6d2   Eric Sandeen   xfs: only return ...
548
  		return 0;
abec5f2bf   Dave Chinner   xfs: split out at...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  	}
  
  	aep = (attrlist_ent_t *)&context->alist[context->firstu];
  	aep->a_valuelen = valuelen;
  	memcpy(aep->a_name, name, namelen);
  	aep->a_name[namelen] = 0;
  	alist->al_offset[context->count++] = context->firstu;
  	alist->al_count = context->count;
  	trace_xfs_attr_list_add(context);
  	return 0;
  }
  
  /*
   * Generate a list of extended attribute names and optionally
   * also value lengths.  Positive return value follows the XFS
   * convention of being an error, zero or negative return code
   * is the length of the buffer returned (negated), indicating
   * success.
   */
  int
  xfs_attr_list(
  	xfs_inode_t	*dp,
  	char		*buffer,
  	int		bufsize,
  	int		flags,
  	attrlist_cursor_kern_t *cursor)
  {
  	xfs_attr_list_context_t context;
  	struct attrlist *alist;
  	int error;
  
  	/*
  	 * Validate the cursor.
  	 */
  	if (cursor->pad1 || cursor->pad2)
2451337dd   Dave Chinner   xfs: global error...
584
  		return -EINVAL;
abec5f2bf   Dave Chinner   xfs: split out at...
585
586
  	if ((cursor->initted == 0) &&
  	    (cursor->hashval || cursor->blkno || cursor->offset))
2451337dd   Dave Chinner   xfs: global error...
587
  		return -EINVAL;
abec5f2bf   Dave Chinner   xfs: split out at...
588
589
590
591
592
  
  	/*
  	 * Check for a properly aligned buffer.
  	 */
  	if (((long)buffer) & (sizeof(int)-1))
2451337dd   Dave Chinner   xfs: global error...
593
  		return -EFAULT;
abec5f2bf   Dave Chinner   xfs: split out at...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  	if (flags & ATTR_KERNOVAL)
  		bufsize = 0;
  
  	/*
  	 * Initialize the output buffer.
  	 */
  	memset(&context, 0, sizeof(context));
  	context.dp = dp;
  	context.cursor = cursor;
  	context.resynch = 1;
  	context.flags = flags;
  	context.alist = buffer;
  	context.bufsize = (bufsize & ~(sizeof(int)-1));  /* align */
  	context.firstu = context.bufsize;
  	context.put_listent = xfs_attr_put_listent;
  
  	alist = (struct attrlist *)context.alist;
  	alist->al_count = 0;
  	alist->al_more = 0;
  	alist->al_offset[0] = context.bufsize;
  
  	error = xfs_attr_list_int(&context);
2451337dd   Dave Chinner   xfs: global error...
616
  	ASSERT(error <= 0);
abec5f2bf   Dave Chinner   xfs: split out at...
617
618
  	return error;
  }