Blame view

fs/ceph/xattr.c 21.2 KB
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
1
  #include <linux/ceph/ceph_debug.h>
355da1eb7   Sage Weil   ceph: inode opera...
2
  #include "super.h"
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
3
4
5
  #include "mds_client.h"
  
  #include <linux/ceph/decode.h>
355da1eb7   Sage Weil   ceph: inode opera...
6
7
  
  #include <linux/xattr.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
8
  #include <linux/slab.h>
355da1eb7   Sage Weil   ceph: inode opera...
9
10
11
  
  static bool ceph_is_valid_xattr(const char *name)
  {
1a7562789   Sage Weil   ceph: use ceph. p...
12
13
  	return !strncmp(name, "ceph.", 5) ||
  	       !strncmp(name, XATTR_SECURITY_PREFIX,
355da1eb7   Sage Weil   ceph: inode opera...
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  			XATTR_SECURITY_PREFIX_LEN) ||
  	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  }
  
  /*
   * These define virtual xattrs exposing the recursive directory
   * statistics and layout metadata.
   */
  struct ceph_vxattr_cb {
  	bool readonly;
  	char *name;
  	size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
  			      size_t size);
  };
  
  /* directories */
  
  static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val,
  					size_t size)
  {
  	return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
  }
  
  static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val,
  				      size_t size)
  {
  	return snprintf(val, size, "%lld", ci->i_files);
  }
  
  static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val,
  					size_t size)
  {
  	return snprintf(val, size, "%lld", ci->i_subdirs);
  }
  
  static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val,
  					 size_t size)
  {
  	return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
  }
  
  static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val,
  				       size_t size)
  {
  	return snprintf(val, size, "%lld", ci->i_rfiles);
  }
  
  static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val,
  					 size_t size)
  {
  	return snprintf(val, size, "%lld", ci->i_rsubdirs);
  }
  
  static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val,
  				       size_t size)
  {
  	return snprintf(val, size, "%lld", ci->i_rbytes);
  }
  
  static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val,
  				       size_t size)
  {
  	return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
  			(long)ci->i_rctime.tv_nsec);
  }
  
  static struct ceph_vxattr_cb ceph_dir_vxattrs[] = {
1a7562789   Sage Weil   ceph: use ceph. p...
82
83
84
85
86
87
88
89
  	{ true, "ceph.dir.entries", ceph_vxattrcb_entries},
  	{ true, "ceph.dir.files", ceph_vxattrcb_files},
  	{ true, "ceph.dir.subdirs", ceph_vxattrcb_subdirs},
  	{ true, "ceph.dir.rentries", ceph_vxattrcb_rentries},
  	{ true, "ceph.dir.rfiles", ceph_vxattrcb_rfiles},
  	{ true, "ceph.dir.rsubdirs", ceph_vxattrcb_rsubdirs},
  	{ true, "ceph.dir.rbytes", ceph_vxattrcb_rbytes},
  	{ true, "ceph.dir.rctime", ceph_vxattrcb_rctime},
355da1eb7   Sage Weil   ceph: inode opera...
90
91
92
93
94
95
96
97
  	{ true, NULL, NULL }
  };
  
  /* files */
  
  static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
  				   size_t size)
  {
b195befd9   Sage Weil   ceph: include pre...
98
99
100
  	int ret;
  
  	ret = snprintf(val, size,
355da1eb7   Sage Weil   ceph: inode opera...
101
102
103
104
105
106
107
  		"chunk_bytes=%lld
  stripe_count=%lld
  object_size=%lld
  ",
  		(unsigned long long)ceph_file_layout_su(ci->i_layout),
  		(unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
  		(unsigned long long)ceph_file_layout_object_size(ci->i_layout));
b195befd9   Sage Weil   ceph: include pre...
108
109
110
111
112
113
  	if (ceph_file_layout_pg_preferred(ci->i_layout))
  		ret += snprintf(val + ret, size, "preferred_osd=%lld
  ",
  			    (unsigned long long)ceph_file_layout_pg_preferred(
  				    ci->i_layout));
  	return ret;
355da1eb7   Sage Weil   ceph: inode opera...
114
115
116
  }
  
  static struct ceph_vxattr_cb ceph_file_vxattrs[] = {
1a7562789   Sage Weil   ceph: use ceph. p...
117
  	{ true, "ceph.layout", ceph_vxattrcb_layout},
355da1eb7   Sage Weil   ceph: inode opera...
118
119
120
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  	{ NULL, NULL }
  };
  
  static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
  {
  	if (S_ISDIR(inode->i_mode))
  		return ceph_dir_vxattrs;
  	else if (S_ISREG(inode->i_mode))
  		return ceph_file_vxattrs;
  	return NULL;
  }
  
  static struct ceph_vxattr_cb *ceph_match_vxattr(struct ceph_vxattr_cb *vxattr,
  						const char *name)
  {
  	do {
  		if (strcmp(vxattr->name, name) == 0)
  			return vxattr;
  		vxattr++;
  	} while (vxattr->name);
  	return NULL;
  }
  
  static int __set_xattr(struct ceph_inode_info *ci,
  			   const char *name, int name_len,
  			   const char *val, int val_len,
  			   int dirty,
  			   int should_free_name, int should_free_val,
  			   struct ceph_inode_xattr **newxattr)
  {
  	struct rb_node **p;
  	struct rb_node *parent = NULL;
  	struct ceph_inode_xattr *xattr = NULL;
  	int c;
  	int new = 0;
  
  	p = &ci->i_xattrs.index.rb_node;
  	while (*p) {
  		parent = *p;
  		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
  		if (c < 0)
  			p = &(*p)->rb_left;
  		else if (c > 0)
  			p = &(*p)->rb_right;
  		else {
  			if (name_len == xattr->name_len)
  				break;
  			else if (name_len < xattr->name_len)
  				p = &(*p)->rb_left;
  			else
  				p = &(*p)->rb_right;
  		}
  		xattr = NULL;
  	}
  
  	if (!xattr) {
  		new = 1;
  		xattr = *newxattr;
  		xattr->name = name;
  		xattr->name_len = name_len;
  		xattr->should_free_name = should_free_name;
  
  		ci->i_xattrs.count++;
  		dout("__set_xattr count=%d
  ", ci->i_xattrs.count);
  	} else {
  		kfree(*newxattr);
  		*newxattr = NULL;
  		if (xattr->should_free_val)
  			kfree((void *)xattr->val);
  
  		if (should_free_name) {
  			kfree((void *)name);
  			name = xattr->name;
  		}
  		ci->i_xattrs.names_size -= xattr->name_len;
  		ci->i_xattrs.vals_size -= xattr->val_len;
  	}
355da1eb7   Sage Weil   ceph: inode opera...
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
223
224
225
226
227
  	ci->i_xattrs.names_size += name_len;
  	ci->i_xattrs.vals_size += val_len;
  	if (val)
  		xattr->val = val;
  	else
  		xattr->val = "";
  
  	xattr->val_len = val_len;
  	xattr->dirty = dirty;
  	xattr->should_free_val = (val && should_free_val);
  
  	if (new) {
  		rb_link_node(&xattr->node, parent, p);
  		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
  		dout("__set_xattr_val p=%p
  ", p);
  	}
  
  	dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s
  ",
  	     ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
  
  	return 0;
  }
  
  static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
  			   const char *name)
  {
  	struct rb_node **p;
  	struct rb_node *parent = NULL;
  	struct ceph_inode_xattr *xattr = NULL;
17db143fc   Sage Weil   ceph: fix xattr r...
228
  	int name_len = strlen(name);
355da1eb7   Sage Weil   ceph: inode opera...
229
230
231
232
233
234
235
  	int c;
  
  	p = &ci->i_xattrs.index.rb_node;
  	while (*p) {
  		parent = *p;
  		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  		c = strncmp(name, xattr->name, xattr->name_len);
17db143fc   Sage Weil   ceph: fix xattr r...
236
237
  		if (c == 0 && name_len > xattr->name_len)
  			c = 1;
355da1eb7   Sage Weil   ceph: inode opera...
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
295
296
297
298
299
300
301
302
303
304
305
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  		if (c < 0)
  			p = &(*p)->rb_left;
  		else if (c > 0)
  			p = &(*p)->rb_right;
  		else {
  			dout("__get_xattr %s: found %.*s
  ", name,
  			     xattr->val_len, xattr->val);
  			return xattr;
  		}
  	}
  
  	dout("__get_xattr %s: not found
  ", name);
  
  	return NULL;
  }
  
  static void __free_xattr(struct ceph_inode_xattr *xattr)
  {
  	BUG_ON(!xattr);
  
  	if (xattr->should_free_name)
  		kfree((void *)xattr->name);
  	if (xattr->should_free_val)
  		kfree((void *)xattr->val);
  
  	kfree(xattr);
  }
  
  static int __remove_xattr(struct ceph_inode_info *ci,
  			  struct ceph_inode_xattr *xattr)
  {
  	if (!xattr)
  		return -EOPNOTSUPP;
  
  	rb_erase(&xattr->node, &ci->i_xattrs.index);
  
  	if (xattr->should_free_name)
  		kfree((void *)xattr->name);
  	if (xattr->should_free_val)
  		kfree((void *)xattr->val);
  
  	ci->i_xattrs.names_size -= xattr->name_len;
  	ci->i_xattrs.vals_size -= xattr->val_len;
  	ci->i_xattrs.count--;
  	kfree(xattr);
  
  	return 0;
  }
  
  static int __remove_xattr_by_name(struct ceph_inode_info *ci,
  			   const char *name)
  {
  	struct rb_node **p;
  	struct ceph_inode_xattr *xattr;
  	int err;
  
  	p = &ci->i_xattrs.index.rb_node;
  	xattr = __get_xattr(ci, name);
  	err = __remove_xattr(ci, xattr);
  	return err;
  }
  
  static char *__copy_xattr_names(struct ceph_inode_info *ci,
  				char *dest)
  {
  	struct rb_node *p;
  	struct ceph_inode_xattr *xattr = NULL;
  
  	p = rb_first(&ci->i_xattrs.index);
  	dout("__copy_xattr_names count=%d
  ", ci->i_xattrs.count);
  
  	while (p) {
  		xattr = rb_entry(p, struct ceph_inode_xattr, node);
  		memcpy(dest, xattr->name, xattr->name_len);
  		dest[xattr->name_len] = '\0';
  
  		dout("dest=%s %p (%s) (%d/%d)
  ", dest, xattr, xattr->name,
  		     xattr->name_len, ci->i_xattrs.names_size);
  
  		dest += xattr->name_len + 1;
  		p = rb_next(p);
  	}
  
  	return dest;
  }
  
  void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
  {
  	struct rb_node *p, *tmp;
  	struct ceph_inode_xattr *xattr = NULL;
  
  	p = rb_first(&ci->i_xattrs.index);
  
  	dout("__ceph_destroy_xattrs p=%p
  ", p);
  
  	while (p) {
  		xattr = rb_entry(p, struct ceph_inode_xattr, node);
  		tmp = p;
  		p = rb_next(tmp);
  		dout("__ceph_destroy_xattrs next p=%p (%.*s)
  ", p,
  		     xattr->name_len, xattr->name);
  		rb_erase(tmp, &ci->i_xattrs.index);
  
  		__free_xattr(xattr);
  	}
  
  	ci->i_xattrs.names_size = 0;
  	ci->i_xattrs.vals_size = 0;
  	ci->i_xattrs.index_version = 0;
  	ci->i_xattrs.count = 0;
  	ci->i_xattrs.index = RB_ROOT;
  }
  
  static int __build_xattrs(struct inode *inode)
be655596b   Sage Weil   ceph: use i_ceph_...
358
359
  	__releases(ci->i_ceph_lock)
  	__acquires(ci->i_ceph_lock)
355da1eb7   Sage Weil   ceph: inode opera...
360
361
362
363
364
365
366
367
368
  {
  	u32 namelen;
  	u32 numattr = 0;
  	void *p, *end;
  	u32 len;
  	const char *name, *val;
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int xattr_version;
  	struct ceph_inode_xattr **xattrs = NULL;
63ff78b25   Sage Weil   ceph: fix uniniti...
369
  	int err = 0;
355da1eb7   Sage Weil   ceph: inode opera...
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  	int i;
  
  	dout("__build_xattrs() len=%d
  ",
  	     ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
  
  	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
  		return 0; /* already built */
  
  	__ceph_destroy_xattrs(ci);
  
  start:
  	/* updated internal xattr rb tree */
  	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
  		p = ci->i_xattrs.blob->vec.iov_base;
  		end = p + ci->i_xattrs.blob->vec.iov_len;
  		ceph_decode_32_safe(&p, end, numattr, bad);
  		xattr_version = ci->i_xattrs.version;
be655596b   Sage Weil   ceph: use i_ceph_...
388
  		spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
389
390
391
392
393
394
395
396
397
398
399
400
401
  
  		xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
  				 GFP_NOFS);
  		err = -ENOMEM;
  		if (!xattrs)
  			goto bad_lock;
  		memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
  		for (i = 0; i < numattr; i++) {
  			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
  					    GFP_NOFS);
  			if (!xattrs[i])
  				goto bad_lock;
  		}
be655596b   Sage Weil   ceph: use i_ceph_...
402
  		spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  		if (ci->i_xattrs.version != xattr_version) {
  			/* lost a race, retry */
  			for (i = 0; i < numattr; i++)
  				kfree(xattrs[i]);
  			kfree(xattrs);
  			goto start;
  		}
  		err = -EIO;
  		while (numattr--) {
  			ceph_decode_32_safe(&p, end, len, bad);
  			namelen = len;
  			name = p;
  			p += len;
  			ceph_decode_32_safe(&p, end, len, bad);
  			val = p;
  			p += len;
  
  			err = __set_xattr(ci, name, namelen, val, len,
  					  0, 0, 0, &xattrs[numattr]);
  
  			if (err < 0)
  				goto bad;
  		}
  		kfree(xattrs);
  	}
  	ci->i_xattrs.index_version = ci->i_xattrs.version;
  	ci->i_xattrs.dirty = false;
  
  	return err;
  bad_lock:
be655596b   Sage Weil   ceph: use i_ceph_...
433
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  bad:
  	if (xattrs) {
  		for (i = 0; i < numattr; i++)
  			kfree(xattrs[i]);
  		kfree(xattrs);
  	}
  	ci->i_xattrs.names_size = 0;
  	return err;
  }
  
  static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
  				    int val_size)
  {
  	/*
  	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
  	 * 4 bytes per each value
  	 */
  	int size = 4 + ci->i_xattrs.count*(4 + 4) +
  			     ci->i_xattrs.names_size +
  			     ci->i_xattrs.vals_size;
  	dout("__get_required_blob_size c=%d names.size=%d vals.size=%d
  ",
  	     ci->i_xattrs.count, ci->i_xattrs.names_size,
  	     ci->i_xattrs.vals_size);
  
  	if (name_size)
  		size += 4 + 4 + name_size + val_size;
  
  	return size;
  }
  
  /*
   * If there are dirty xattrs, reencode xattrs into the prealloc_blob
   * and swap into place.
   */
  void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
  {
  	struct rb_node *p;
  	struct ceph_inode_xattr *xattr = NULL;
  	void *dest;
  
  	dout("__build_xattrs_blob %p
  ", &ci->vfs_inode);
  	if (ci->i_xattrs.dirty) {
  		int need = __get_required_blob_size(ci, 0, 0);
  
  		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
  
  		p = rb_first(&ci->i_xattrs.index);
  		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
  
  		ceph_encode_32(&dest, ci->i_xattrs.count);
  		while (p) {
  			xattr = rb_entry(p, struct ceph_inode_xattr, node);
  
  			ceph_encode_32(&dest, xattr->name_len);
  			memcpy(dest, xattr->name, xattr->name_len);
  			dest += xattr->name_len;
  			ceph_encode_32(&dest, xattr->val_len);
  			memcpy(dest, xattr->val, xattr->val_len);
  			dest += xattr->val_len;
  
  			p = rb_next(p);
  		}
  
  		/* adjust buffer len; it may be larger than we need */
  		ci->i_xattrs.prealloc_blob->vec.iov_len =
  			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
b6c1d5b81   Sage Weil   ceph: simplify ce...
502
503
  		if (ci->i_xattrs.blob)
  			ceph_buffer_put(ci->i_xattrs.blob);
355da1eb7   Sage Weil   ceph: inode opera...
504
505
506
  		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
  		ci->i_xattrs.prealloc_blob = NULL;
  		ci->i_xattrs.dirty = false;
4a625be47   Sage Weil   ceph: include dir...
507
  		ci->i_xattrs.version++;
355da1eb7   Sage Weil   ceph: inode opera...
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  	}
  }
  
  ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
  		      size_t size)
  {
  	struct inode *inode = dentry->d_inode;
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  	int err;
  	struct ceph_inode_xattr *xattr;
  	struct ceph_vxattr_cb *vxattr = NULL;
  
  	if (!ceph_is_valid_xattr(name))
  		return -ENODATA;
  
  	/* let's see if a virtual xattr was requested */
  	if (vxattrs)
  		vxattr = ceph_match_vxattr(vxattrs, name);
be655596b   Sage Weil   ceph: use i_ceph_...
527
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
528
529
530
531
532
533
534
535
  	dout("getxattr %p ver=%lld index_ver=%lld
  ", inode,
  	     ci->i_xattrs.version, ci->i_xattrs.index_version);
  
  	if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
  	    (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
  		goto get_xattr;
  	} else {
be655596b   Sage Weil   ceph: use i_ceph_...
536
  		spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
537
538
539
540
541
  		/* get xattrs from mds (if we don't already have them) */
  		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  		if (err)
  			return err;
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
542
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
  
  	if (vxattr && vxattr->readonly) {
  		err = vxattr->getxattr_cb(ci, value, size);
  		goto out;
  	}
  
  	err = __build_xattrs(inode);
  	if (err < 0)
  		goto out;
  
  get_xattr:
  	err = -ENODATA;  /* == ENOATTR */
  	xattr = __get_xattr(ci, name);
  	if (!xattr) {
  		if (vxattr)
  			err = vxattr->getxattr_cb(ci, value, size);
  		goto out;
  	}
  
  	err = -ERANGE;
  	if (size && size < xattr->val_len)
  		goto out;
  
  	err = xattr->val_len;
  	if (size == 0)
  		goto out;
  
  	memcpy(value, xattr->val, xattr->val_len);
  
  out:
be655596b   Sage Weil   ceph: use i_ceph_...
573
  	spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
574
575
576
577
578
579
580
581
582
583
584
585
586
  	return err;
  }
  
  ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
  {
  	struct inode *inode = dentry->d_inode;
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  	u32 vir_namelen = 0;
  	u32 namelen;
  	int err;
  	u32 len;
  	int i;
be655596b   Sage Weil   ceph: use i_ceph_...
587
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
588
589
590
591
592
  	dout("listxattr %p ver=%lld index_ver=%lld
  ", inode,
  	     ci->i_xattrs.version, ci->i_xattrs.index_version);
  
  	if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
bddfa3cc1   Henry C Chang   ceph: listxattr s...
593
  	    (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
355da1eb7   Sage Weil   ceph: inode opera...
594
595
  		goto list_xattr;
  	} else {
be655596b   Sage Weil   ceph: use i_ceph_...
596
  		spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
597
598
599
600
  		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  		if (err)
  			return err;
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
601
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  
  	err = __build_xattrs(inode);
  	if (err < 0)
  		goto out;
  
  list_xattr:
  	vir_namelen = 0;
  	/* include virtual dir xattrs */
  	if (vxattrs)
  		for (i = 0; vxattrs[i].name; i++)
  			vir_namelen += strlen(vxattrs[i].name) + 1;
  	/* adding 1 byte per each variable due to the null termination */
  	namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
  	err = -ERANGE;
  	if (size && namelen > size)
  		goto out;
  
  	err = namelen;
  	if (size == 0)
  		goto out;
  
  	names = __copy_xattr_names(ci, names);
  
  	/* virtual xattr names, too */
  	if (vxattrs)
  		for (i = 0; vxattrs[i].name; i++) {
  			len = sprintf(names, "%s", vxattrs[i].name);
  			names += len + 1;
  		}
  
  out:
be655596b   Sage Weil   ceph: use i_ceph_...
633
  	spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
634
635
636
637
638
639
  	return err;
  }
  
  static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
  			      const char *value, size_t size, int flags)
  {
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
640
  	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
355da1eb7   Sage Weil   ceph: inode opera...
641
642
  	struct inode *inode = dentry->d_inode;
  	struct ceph_inode_info *ci = ceph_inode(inode);
5f21c96dd   Sage Weil   ceph: protect acc...
643
  	struct inode *parent_inode;
355da1eb7   Sage Weil   ceph: inode opera...
644
  	struct ceph_mds_request *req;
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
645
  	struct ceph_mds_client *mdsc = fsc->mdsc;
355da1eb7   Sage Weil   ceph: inode opera...
646
647
648
649
650
651
652
653
654
655
656
657
658
  	int err;
  	int i, nr_pages;
  	struct page **pages = NULL;
  	void *kaddr;
  
  	/* copy value into some pages */
  	nr_pages = calc_pages_for(0, size);
  	if (nr_pages) {
  		pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
  		if (!pages)
  			return -ENOMEM;
  		err = -ENOMEM;
  		for (i = 0; i < nr_pages; i++) {
31459fe4b   Yehuda Sadeh   ceph: use __page_...
659
  			pages[i] = __page_cache_alloc(GFP_NOFS);
355da1eb7   Sage Weil   ceph: inode opera...
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
  			if (!pages[i]) {
  				nr_pages = i;
  				goto out;
  			}
  			kaddr = kmap(pages[i]);
  			memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
  			       min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
  		}
  	}
  
  	dout("setxattr value=%.*s
  ", (int)size, value);
  
  	/* do request */
  	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
  				       USE_AUTH_MDS);
60d877334   Julia Lawall   fs/ceph: introduc...
676
677
678
679
  	if (IS_ERR(req)) {
  		err = PTR_ERR(req);
  		goto out;
  	}
70b666c3b   Sage Weil   ceph: use ihold w...
680
681
  	req->r_inode = inode;
  	ihold(inode);
355da1eb7   Sage Weil   ceph: inode opera...
682
683
684
685
686
687
688
689
690
691
692
  	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  	req->r_num_caps = 1;
  	req->r_args.setxattr.flags = cpu_to_le32(flags);
  	req->r_path2 = kstrdup(name, GFP_NOFS);
  
  	req->r_pages = pages;
  	req->r_num_pages = nr_pages;
  	req->r_data_len = size;
  
  	dout("xattr.ver (before): %lld
  ", ci->i_xattrs.version);
5f21c96dd   Sage Weil   ceph: protect acc...
693
  	parent_inode = ceph_get_dentry_parent_inode(dentry);
355da1eb7   Sage Weil   ceph: inode opera...
694
  	err = ceph_mdsc_do_request(mdsc, parent_inode, req);
5f21c96dd   Sage Weil   ceph: protect acc...
695
  	iput(parent_inode);
355da1eb7   Sage Weil   ceph: inode opera...
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
  	ceph_mdsc_put_request(req);
  	dout("xattr.ver (after): %lld
  ", ci->i_xattrs.version);
  
  out:
  	if (pages) {
  		for (i = 0; i < nr_pages; i++)
  			__free_page(pages[i]);
  		kfree(pages);
  	}
  	return err;
  }
  
  int ceph_setxattr(struct dentry *dentry, const char *name,
  		  const void *value, size_t size, int flags)
  {
  	struct inode *inode = dentry->d_inode;
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  	int err;
  	int name_len = strlen(name);
  	int val_len = size;
  	char *newname = NULL;
  	char *newval = NULL;
  	struct ceph_inode_xattr *xattr = NULL;
  	int issued;
  	int required_blob_size;
fca65b4ad   Sage Weil   ceph: do not call...
723
  	int dirty;
355da1eb7   Sage Weil   ceph: inode opera...
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
  
  	if (ceph_snap(inode) != CEPH_NOSNAP)
  		return -EROFS;
  
  	if (!ceph_is_valid_xattr(name))
  		return -EOPNOTSUPP;
  
  	if (vxattrs) {
  		struct ceph_vxattr_cb *vxattr =
  			ceph_match_vxattr(vxattrs, name);
  		if (vxattr && vxattr->readonly)
  			return -EOPNOTSUPP;
  	}
  
  	/* preallocate memory for xattr name, value, index node */
  	err = -ENOMEM;
61413c2f5   Julia Lawall   fs/ceph/xattr.c: ...
740
  	newname = kmemdup(name, name_len + 1, GFP_NOFS);
355da1eb7   Sage Weil   ceph: inode opera...
741
742
  	if (!newname)
  		goto out;
355da1eb7   Sage Weil   ceph: inode opera...
743
744
745
746
747
748
749
750
751
752
753
754
  
  	if (val_len) {
  		newval = kmalloc(val_len + 1, GFP_NOFS);
  		if (!newval)
  			goto out;
  		memcpy(newval, value, val_len);
  		newval[val_len] = '\0';
  	}
  
  	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
  	if (!xattr)
  		goto out;
be655596b   Sage Weil   ceph: use i_ceph_...
755
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
756
757
758
759
760
761
762
763
764
765
766
  retry:
  	issued = __ceph_caps_issued(ci, NULL);
  	if (!(issued & CEPH_CAP_XATTR_EXCL))
  		goto do_sync;
  	__build_xattrs(inode);
  
  	required_blob_size = __get_required_blob_size(ci, name_len, val_len);
  
  	if (!ci->i_xattrs.prealloc_blob ||
  	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  		struct ceph_buffer *blob = NULL;
be655596b   Sage Weil   ceph: use i_ceph_...
767
  		spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
768
769
  		dout(" preaallocating new blob size=%d
  ", required_blob_size);
b6c1d5b81   Sage Weil   ceph: simplify ce...
770
  		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
355da1eb7   Sage Weil   ceph: inode opera...
771
772
  		if (!blob)
  			goto out;
be655596b   Sage Weil   ceph: use i_ceph_...
773
  		spin_lock(&ci->i_ceph_lock);
b6c1d5b81   Sage Weil   ceph: simplify ce...
774
775
  		if (ci->i_xattrs.prealloc_blob)
  			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
355da1eb7   Sage Weil   ceph: inode opera...
776
777
778
779
780
781
782
783
  		ci->i_xattrs.prealloc_blob = blob;
  		goto retry;
  	}
  
  	dout("setxattr %p issued %s
  ", inode, ceph_cap_string(issued));
  	err = __set_xattr(ci, newname, name_len, newval,
  			  val_len, 1, 1, 1, &xattr);
fca65b4ad   Sage Weil   ceph: do not call...
784
  	dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
355da1eb7   Sage Weil   ceph: inode opera...
785
786
  	ci->i_xattrs.dirty = true;
  	inode->i_ctime = CURRENT_TIME;
be655596b   Sage Weil   ceph: use i_ceph_...
787
  	spin_unlock(&ci->i_ceph_lock);
fca65b4ad   Sage Weil   ceph: do not call...
788
789
  	if (dirty)
  		__mark_inode_dirty(inode, dirty);
355da1eb7   Sage Weil   ceph: inode opera...
790
791
792
  	return err;
  
  do_sync:
be655596b   Sage Weil   ceph: use i_ceph_...
793
  	spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
794
795
796
797
798
799
800
801
802
803
  	err = ceph_sync_setxattr(dentry, name, value, size, flags);
  out:
  	kfree(newname);
  	kfree(newval);
  	kfree(xattr);
  	return err;
  }
  
  static int ceph_send_removexattr(struct dentry *dentry, const char *name)
  {
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
804
805
  	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  	struct ceph_mds_client *mdsc = fsc->mdsc;
355da1eb7   Sage Weil   ceph: inode opera...
806
  	struct inode *inode = dentry->d_inode;
5f21c96dd   Sage Weil   ceph: protect acc...
807
  	struct inode *parent_inode;
355da1eb7   Sage Weil   ceph: inode opera...
808
809
810
811
812
813
814
  	struct ceph_mds_request *req;
  	int err;
  
  	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
  				       USE_AUTH_MDS);
  	if (IS_ERR(req))
  		return PTR_ERR(req);
70b666c3b   Sage Weil   ceph: use ihold w...
815
816
  	req->r_inode = inode;
  	ihold(inode);
355da1eb7   Sage Weil   ceph: inode opera...
817
818
819
  	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  	req->r_num_caps = 1;
  	req->r_path2 = kstrdup(name, GFP_NOFS);
5f21c96dd   Sage Weil   ceph: protect acc...
820
  	parent_inode = ceph_get_dentry_parent_inode(dentry);
355da1eb7   Sage Weil   ceph: inode opera...
821
  	err = ceph_mdsc_do_request(mdsc, parent_inode, req);
5f21c96dd   Sage Weil   ceph: protect acc...
822
  	iput(parent_inode);
355da1eb7   Sage Weil   ceph: inode opera...
823
824
825
826
827
828
829
830
831
832
833
  	ceph_mdsc_put_request(req);
  	return err;
  }
  
  int ceph_removexattr(struct dentry *dentry, const char *name)
  {
  	struct inode *inode = dentry->d_inode;
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  	int issued;
  	int err;
83eb26af0   Alex Elder   ceph: ensure prea...
834
  	int required_blob_size;
fca65b4ad   Sage Weil   ceph: do not call...
835
  	int dirty;
355da1eb7   Sage Weil   ceph: inode opera...
836
837
838
839
840
841
842
843
844
845
846
847
848
  
  	if (ceph_snap(inode) != CEPH_NOSNAP)
  		return -EROFS;
  
  	if (!ceph_is_valid_xattr(name))
  		return -EOPNOTSUPP;
  
  	if (vxattrs) {
  		struct ceph_vxattr_cb *vxattr =
  			ceph_match_vxattr(vxattrs, name);
  		if (vxattr && vxattr->readonly)
  			return -EOPNOTSUPP;
  	}
83eb26af0   Alex Elder   ceph: ensure prea...
849
  	err = -ENOMEM;
be655596b   Sage Weil   ceph: use i_ceph_...
850
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
851
  	__build_xattrs(inode);
83eb26af0   Alex Elder   ceph: ensure prea...
852
  retry:
355da1eb7   Sage Weil   ceph: inode opera...
853
854
855
856
857
858
  	issued = __ceph_caps_issued(ci, NULL);
  	dout("removexattr %p issued %s
  ", inode, ceph_cap_string(issued));
  
  	if (!(issued & CEPH_CAP_XATTR_EXCL))
  		goto do_sync;
83eb26af0   Alex Elder   ceph: ensure prea...
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
  	required_blob_size = __get_required_blob_size(ci, 0, 0);
  
  	if (!ci->i_xattrs.prealloc_blob ||
  	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  		struct ceph_buffer *blob;
  
  		spin_unlock(&ci->i_ceph_lock);
  		dout(" preaallocating new blob size=%d
  ", required_blob_size);
  		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  		if (!blob)
  			goto out;
  		spin_lock(&ci->i_ceph_lock);
  		if (ci->i_xattrs.prealloc_blob)
  			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  		ci->i_xattrs.prealloc_blob = blob;
  		goto retry;
  	}
355da1eb7   Sage Weil   ceph: inode opera...
877
  	err = __remove_xattr_by_name(ceph_inode(inode), name);
fca65b4ad   Sage Weil   ceph: do not call...
878
  	dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
355da1eb7   Sage Weil   ceph: inode opera...
879
880
  	ci->i_xattrs.dirty = true;
  	inode->i_ctime = CURRENT_TIME;
be655596b   Sage Weil   ceph: use i_ceph_...
881
  	spin_unlock(&ci->i_ceph_lock);
fca65b4ad   Sage Weil   ceph: do not call...
882
883
  	if (dirty)
  		__mark_inode_dirty(inode, dirty);
355da1eb7   Sage Weil   ceph: inode opera...
884
885
  	return err;
  do_sync:
be655596b   Sage Weil   ceph: use i_ceph_...
886
  	spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
887
  	err = ceph_send_removexattr(dentry, name);
83eb26af0   Alex Elder   ceph: ensure prea...
888
  out:
355da1eb7   Sage Weil   ceph: inode opera...
889
890
  	return err;
  }