xattr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. File: fs/xattr.c
  3. Extended attribute handling.
  4. Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
  5. Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
  6. Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/file.h>
  11. #include <linux/xattr.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/security.h>
  15. #include <linux/evm.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/export.h>
  18. #include <linux/fsnotify.h>
  19. #include <linux/audit.h>
  20. #include <linux/vmalloc.h>
  21. #include <asm/uaccess.h>
  22. /*
  23. * Check permissions for extended attribute access. This is a bit complicated
  24. * because different namespaces have very different rules.
  25. */
  26. static int
  27. xattr_permission(struct inode *inode, const char *name, int mask)
  28. {
  29. /*
  30. * We can never set or remove an extended attribute on a read-only
  31. * filesystem or on an immutable / append-only inode.
  32. */
  33. if (mask & MAY_WRITE) {
  34. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  35. return -EPERM;
  36. }
  37. /*
  38. * No restriction for security.* and system.* from the VFS. Decision
  39. * on these is left to the underlying filesystem / security module.
  40. */
  41. if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
  42. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  43. return 0;
  44. /*
  45. * The trusted.* namespace can only be accessed by privileged users.
  46. */
  47. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
  48. if (!capable(CAP_SYS_ADMIN))
  49. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  50. return 0;
  51. }
  52. /*
  53. * In the user.* namespace, only regular files and directories can have
  54. * extended attributes. For sticky directories, only the owner and
  55. * privileged users can write attributes.
  56. */
  57. if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
  58. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  59. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  60. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  61. (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
  62. return -EPERM;
  63. }
  64. return inode_permission2(ERR_PTR(-EOPNOTSUPP), inode, mask);
  65. }
  66. /**
  67. * __vfs_setxattr_noperm - perform setxattr operation without performing
  68. * permission checks.
  69. *
  70. * @dentry - object to perform setxattr on
  71. * @name - xattr name to set
  72. * @value - value to set @name to
  73. * @size - size of @value
  74. * @flags - flags to pass into filesystem operations
  75. *
  76. * returns the result of the internal setxattr or setsecurity operations.
  77. *
  78. * This function requires the caller to lock the inode's i_mutex before it
  79. * is executed. It also assumes that the caller will make the appropriate
  80. * permission checks.
  81. */
  82. int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
  83. const void *value, size_t size, int flags)
  84. {
  85. struct inode *inode = dentry->d_inode;
  86. int error = -EOPNOTSUPP;
  87. int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
  88. XATTR_SECURITY_PREFIX_LEN);
  89. if (issec)
  90. inode->i_flags &= ~S_NOSEC;
  91. if (inode->i_op->setxattr) {
  92. error = inode->i_op->setxattr(dentry, name, value, size, flags);
  93. if (!error) {
  94. fsnotify_xattr(dentry);
  95. security_inode_post_setxattr(dentry, name, value,
  96. size, flags);
  97. }
  98. } else if (issec) {
  99. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  100. error = security_inode_setsecurity(inode, suffix, value,
  101. size, flags);
  102. if (!error)
  103. fsnotify_xattr(dentry);
  104. }
  105. return error;
  106. }
  107. int
  108. vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  109. size_t size, int flags)
  110. {
  111. struct inode *inode = dentry->d_inode;
  112. int error;
  113. error = xattr_permission(inode, name, MAY_WRITE);
  114. if (error)
  115. return error;
  116. mutex_lock(&inode->i_mutex);
  117. error = security_inode_setxattr(dentry, name, value, size, flags);
  118. if (error)
  119. goto out;
  120. error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
  121. out:
  122. mutex_unlock(&inode->i_mutex);
  123. return error;
  124. }
  125. EXPORT_SYMBOL_GPL(vfs_setxattr);
  126. ssize_t
  127. xattr_getsecurity(struct inode *inode, const char *name, void *value,
  128. size_t size)
  129. {
  130. void *buffer = NULL;
  131. ssize_t len;
  132. if (!value || !size) {
  133. len = security_inode_getsecurity(inode, name, &buffer, false);
  134. goto out_noalloc;
  135. }
  136. len = security_inode_getsecurity(inode, name, &buffer, true);
  137. if (len < 0)
  138. return len;
  139. if (size < len) {
  140. len = -ERANGE;
  141. goto out;
  142. }
  143. memcpy(value, buffer, len);
  144. out:
  145. security_release_secctx(buffer, len);
  146. out_noalloc:
  147. return len;
  148. }
  149. EXPORT_SYMBOL_GPL(xattr_getsecurity);
  150. /*
  151. * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
  152. *
  153. * Allocate memory, if not already allocated, or re-allocate correct size,
  154. * before retrieving the extended attribute.
  155. *
  156. * Returns the result of alloc, if failed, or the getxattr operation.
  157. */
  158. ssize_t
  159. vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
  160. size_t xattr_size, gfp_t flags)
  161. {
  162. struct inode *inode = dentry->d_inode;
  163. char *value = *xattr_value;
  164. int error;
  165. error = xattr_permission(inode, name, MAY_READ);
  166. if (error)
  167. return error;
  168. if (!inode->i_op->getxattr)
  169. return -EOPNOTSUPP;
  170. error = inode->i_op->getxattr(dentry, name, NULL, 0);
  171. if (error < 0)
  172. return error;
  173. if (!value || (error > xattr_size)) {
  174. value = krealloc(*xattr_value, error + 1, flags);
  175. if (!value)
  176. return -ENOMEM;
  177. memset(value, 0, error + 1);
  178. }
  179. error = inode->i_op->getxattr(dentry, name, value, error);
  180. *xattr_value = value;
  181. return error;
  182. }
  183. /* Compare an extended attribute value with the given value */
  184. int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
  185. const char *value, size_t size, gfp_t flags)
  186. {
  187. char *xattr_value = NULL;
  188. int rc;
  189. rc = vfs_getxattr_alloc(dentry, xattr_name, &xattr_value, 0, flags);
  190. if (rc < 0)
  191. return rc;
  192. if ((rc != size) || (memcmp(xattr_value, value, rc) != 0))
  193. rc = -EINVAL;
  194. else
  195. rc = 0;
  196. kfree(xattr_value);
  197. return rc;
  198. }
  199. ssize_t
  200. vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
  201. {
  202. struct inode *inode = dentry->d_inode;
  203. int error;
  204. error = xattr_permission(inode, name, MAY_READ);
  205. if (error)
  206. return error;
  207. error = security_inode_getxattr(dentry, name);
  208. if (error)
  209. return error;
  210. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  211. XATTR_SECURITY_PREFIX_LEN)) {
  212. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  213. int ret = xattr_getsecurity(inode, suffix, value, size);
  214. /*
  215. * Only overwrite the return value if a security module
  216. * is actually active.
  217. */
  218. if (ret == -EOPNOTSUPP)
  219. goto nolsm;
  220. return ret;
  221. }
  222. nolsm:
  223. if (inode->i_op->getxattr)
  224. error = inode->i_op->getxattr(dentry, name, value, size);
  225. else
  226. error = -EOPNOTSUPP;
  227. return error;
  228. }
  229. EXPORT_SYMBOL_GPL(vfs_getxattr);
  230. ssize_t
  231. vfs_listxattr(struct dentry *d, char *list, size_t size)
  232. {
  233. ssize_t error;
  234. error = security_inode_listxattr(d);
  235. if (error)
  236. return error;
  237. error = -EOPNOTSUPP;
  238. if (d->d_inode->i_op->listxattr) {
  239. error = d->d_inode->i_op->listxattr(d, list, size);
  240. } else {
  241. error = security_inode_listsecurity(d->d_inode, list, size);
  242. if (size && error > size)
  243. error = -ERANGE;
  244. }
  245. return error;
  246. }
  247. EXPORT_SYMBOL_GPL(vfs_listxattr);
  248. int
  249. vfs_removexattr(struct dentry *dentry, const char *name)
  250. {
  251. struct inode *inode = dentry->d_inode;
  252. int error;
  253. if (!inode->i_op->removexattr)
  254. return -EOPNOTSUPP;
  255. error = xattr_permission(inode, name, MAY_WRITE);
  256. if (error)
  257. return error;
  258. error = security_inode_removexattr(dentry, name);
  259. if (error)
  260. return error;
  261. mutex_lock(&inode->i_mutex);
  262. error = inode->i_op->removexattr(dentry, name);
  263. mutex_unlock(&inode->i_mutex);
  264. if (!error) {
  265. fsnotify_xattr(dentry);
  266. evm_inode_post_removexattr(dentry, name);
  267. }
  268. return error;
  269. }
  270. EXPORT_SYMBOL_GPL(vfs_removexattr);
  271. /*
  272. * Extended attribute SET operations
  273. */
  274. static long
  275. setxattr(struct dentry *d, const char __user *name, const void __user *value,
  276. size_t size, int flags)
  277. {
  278. int error;
  279. void *kvalue = NULL;
  280. void *vvalue = NULL; /* If non-NULL, we used vmalloc() */
  281. char kname[XATTR_NAME_MAX + 1];
  282. if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
  283. return -EINVAL;
  284. error = strncpy_from_user(kname, name, sizeof(kname));
  285. if (error == 0 || error == sizeof(kname))
  286. error = -ERANGE;
  287. if (error < 0)
  288. return error;
  289. if (size) {
  290. if (size > XATTR_SIZE_MAX)
  291. return -E2BIG;
  292. kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
  293. if (!kvalue) {
  294. vvalue = vmalloc(size);
  295. if (!vvalue)
  296. return -ENOMEM;
  297. kvalue = vvalue;
  298. }
  299. if (copy_from_user(kvalue, value, size)) {
  300. error = -EFAULT;
  301. goto out;
  302. }
  303. }
  304. error = vfs_setxattr(d, kname, kvalue, size, flags);
  305. out:
  306. if (vvalue)
  307. vfree(vvalue);
  308. else
  309. kfree(kvalue);
  310. return error;
  311. }
  312. SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
  313. const char __user *, name, const void __user *, value,
  314. size_t, size, int, flags)
  315. {
  316. struct path path;
  317. int error;
  318. error = user_path(pathname, &path);
  319. if (error)
  320. return error;
  321. error = mnt_want_write(path.mnt);
  322. if (!error) {
  323. error = setxattr(path.dentry, name, value, size, flags);
  324. mnt_drop_write(path.mnt);
  325. }
  326. path_put(&path);
  327. return error;
  328. }
  329. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  330. const char __user *, name, const void __user *, value,
  331. size_t, size, int, flags)
  332. {
  333. struct path path;
  334. int error;
  335. error = user_lpath(pathname, &path);
  336. if (error)
  337. return error;
  338. error = mnt_want_write(path.mnt);
  339. if (!error) {
  340. error = setxattr(path.dentry, name, value, size, flags);
  341. mnt_drop_write(path.mnt);
  342. }
  343. path_put(&path);
  344. return error;
  345. }
  346. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  347. const void __user *,value, size_t, size, int, flags)
  348. {
  349. struct file *f;
  350. struct dentry *dentry;
  351. int error = -EBADF;
  352. f = fget(fd);
  353. if (!f)
  354. return error;
  355. dentry = f->f_path.dentry;
  356. audit_inode(NULL, dentry);
  357. error = mnt_want_write_file(f);
  358. if (!error) {
  359. error = setxattr(dentry, name, value, size, flags);
  360. mnt_drop_write_file(f);
  361. }
  362. fput(f);
  363. return error;
  364. }
  365. /*
  366. * Extended attribute GET operations
  367. */
  368. static ssize_t
  369. getxattr(struct dentry *d, const char __user *name, void __user *value,
  370. size_t size)
  371. {
  372. ssize_t error;
  373. void *kvalue = NULL;
  374. char kname[XATTR_NAME_MAX + 1];
  375. error = strncpy_from_user(kname, name, sizeof(kname));
  376. if (error == 0 || error == sizeof(kname))
  377. error = -ERANGE;
  378. if (error < 0)
  379. return error;
  380. if (size) {
  381. if (size > XATTR_SIZE_MAX)
  382. size = XATTR_SIZE_MAX;
  383. kvalue = kzalloc(size, GFP_KERNEL);
  384. if (!kvalue)
  385. return -ENOMEM;
  386. }
  387. error = vfs_getxattr(d, kname, kvalue, size);
  388. if (error > 0) {
  389. if (size && copy_to_user(value, kvalue, error))
  390. error = -EFAULT;
  391. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  392. /* The file system tried to returned a value bigger
  393. than XATTR_SIZE_MAX bytes. Not possible. */
  394. error = -E2BIG;
  395. }
  396. kfree(kvalue);
  397. return error;
  398. }
  399. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  400. const char __user *, name, void __user *, value, size_t, size)
  401. {
  402. struct path path;
  403. ssize_t error;
  404. error = user_path(pathname, &path);
  405. if (error)
  406. return error;
  407. error = getxattr(path.dentry, name, value, size);
  408. path_put(&path);
  409. return error;
  410. }
  411. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  412. const char __user *, name, void __user *, value, size_t, size)
  413. {
  414. struct path path;
  415. ssize_t error;
  416. error = user_lpath(pathname, &path);
  417. if (error)
  418. return error;
  419. error = getxattr(path.dentry, name, value, size);
  420. path_put(&path);
  421. return error;
  422. }
  423. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  424. void __user *, value, size_t, size)
  425. {
  426. struct file *f;
  427. ssize_t error = -EBADF;
  428. f = fget(fd);
  429. if (!f)
  430. return error;
  431. audit_inode(NULL, f->f_path.dentry);
  432. error = getxattr(f->f_path.dentry, name, value, size);
  433. fput(f);
  434. return error;
  435. }
  436. /*
  437. * Extended attribute LIST operations
  438. */
  439. static ssize_t
  440. listxattr(struct dentry *d, char __user *list, size_t size)
  441. {
  442. ssize_t error;
  443. char *klist = NULL;
  444. char *vlist = NULL; /* If non-NULL, we used vmalloc() */
  445. if (size) {
  446. if (size > XATTR_LIST_MAX)
  447. size = XATTR_LIST_MAX;
  448. klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
  449. if (!klist) {
  450. vlist = vmalloc(size);
  451. if (!vlist)
  452. return -ENOMEM;
  453. klist = vlist;
  454. }
  455. }
  456. error = vfs_listxattr(d, klist, size);
  457. if (error > 0) {
  458. if (size && copy_to_user(list, klist, error))
  459. error = -EFAULT;
  460. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  461. /* The file system tried to returned a list bigger
  462. than XATTR_LIST_MAX bytes. Not possible. */
  463. error = -E2BIG;
  464. }
  465. if (vlist)
  466. vfree(vlist);
  467. else
  468. kfree(klist);
  469. return error;
  470. }
  471. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  472. size_t, size)
  473. {
  474. struct path path;
  475. ssize_t error;
  476. error = user_path(pathname, &path);
  477. if (error)
  478. return error;
  479. error = listxattr(path.dentry, list, size);
  480. path_put(&path);
  481. return error;
  482. }
  483. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  484. size_t, size)
  485. {
  486. struct path path;
  487. ssize_t error;
  488. error = user_lpath(pathname, &path);
  489. if (error)
  490. return error;
  491. error = listxattr(path.dentry, list, size);
  492. path_put(&path);
  493. return error;
  494. }
  495. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  496. {
  497. struct file *f;
  498. ssize_t error = -EBADF;
  499. f = fget(fd);
  500. if (!f)
  501. return error;
  502. audit_inode(NULL, f->f_path.dentry);
  503. error = listxattr(f->f_path.dentry, list, size);
  504. fput(f);
  505. return error;
  506. }
  507. /*
  508. * Extended attribute REMOVE operations
  509. */
  510. static long
  511. removexattr(struct dentry *d, const char __user *name)
  512. {
  513. int error;
  514. char kname[XATTR_NAME_MAX + 1];
  515. error = strncpy_from_user(kname, name, sizeof(kname));
  516. if (error == 0 || error == sizeof(kname))
  517. error = -ERANGE;
  518. if (error < 0)
  519. return error;
  520. return vfs_removexattr(d, kname);
  521. }
  522. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  523. const char __user *, name)
  524. {
  525. struct path path;
  526. int error;
  527. error = user_path(pathname, &path);
  528. if (error)
  529. return error;
  530. error = mnt_want_write(path.mnt);
  531. if (!error) {
  532. error = removexattr(path.dentry, name);
  533. mnt_drop_write(path.mnt);
  534. }
  535. path_put(&path);
  536. return error;
  537. }
  538. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  539. const char __user *, name)
  540. {
  541. struct path path;
  542. int error;
  543. error = user_lpath(pathname, &path);
  544. if (error)
  545. return error;
  546. error = mnt_want_write(path.mnt);
  547. if (!error) {
  548. error = removexattr(path.dentry, name);
  549. mnt_drop_write(path.mnt);
  550. }
  551. path_put(&path);
  552. return error;
  553. }
  554. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  555. {
  556. struct file *f;
  557. struct dentry *dentry;
  558. int error = -EBADF;
  559. f = fget(fd);
  560. if (!f)
  561. return error;
  562. dentry = f->f_path.dentry;
  563. audit_inode(NULL, dentry);
  564. error = mnt_want_write_file(f);
  565. if (!error) {
  566. error = removexattr(dentry, name);
  567. mnt_drop_write_file(f);
  568. }
  569. fput(f);
  570. return error;
  571. }
  572. static const char *
  573. strcmp_prefix(const char *a, const char *a_prefix)
  574. {
  575. while (*a_prefix && *a == *a_prefix) {
  576. a++;
  577. a_prefix++;
  578. }
  579. return *a_prefix ? NULL : a;
  580. }
  581. /*
  582. * In order to implement different sets of xattr operations for each xattr
  583. * prefix with the generic xattr API, a filesystem should create a
  584. * null-terminated array of struct xattr_handler (one for each prefix) and
  585. * hang a pointer to it off of the s_xattr field of the superblock.
  586. *
  587. * The generic_fooxattr() functions will use this list to dispatch xattr
  588. * operations to the correct xattr_handler.
  589. */
  590. #define for_each_xattr_handler(handlers, handler) \
  591. for ((handler) = *(handlers)++; \
  592. (handler) != NULL; \
  593. (handler) = *(handlers)++)
  594. /*
  595. * Find the xattr_handler with the matching prefix.
  596. */
  597. static const struct xattr_handler *
  598. xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
  599. {
  600. const struct xattr_handler *handler;
  601. if (!*name)
  602. return NULL;
  603. for_each_xattr_handler(handlers, handler) {
  604. const char *n = strcmp_prefix(*name, handler->prefix);
  605. if (n) {
  606. *name = n;
  607. break;
  608. }
  609. }
  610. return handler;
  611. }
  612. /*
  613. * Find the handler for the prefix and dispatch its get() operation.
  614. */
  615. ssize_t
  616. generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
  617. {
  618. const struct xattr_handler *handler;
  619. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  620. if (!handler)
  621. return -EOPNOTSUPP;
  622. return handler->get(dentry, name, buffer, size, handler->flags);
  623. }
  624. /*
  625. * Combine the results of the list() operation from every xattr_handler in the
  626. * list.
  627. */
  628. ssize_t
  629. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  630. {
  631. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  632. unsigned int size = 0;
  633. if (!buffer) {
  634. for_each_xattr_handler(handlers, handler) {
  635. size += handler->list(dentry, NULL, 0, NULL, 0,
  636. handler->flags);
  637. }
  638. } else {
  639. char *buf = buffer;
  640. for_each_xattr_handler(handlers, handler) {
  641. size = handler->list(dentry, buf, buffer_size,
  642. NULL, 0, handler->flags);
  643. if (size > buffer_size)
  644. return -ERANGE;
  645. buf += size;
  646. buffer_size -= size;
  647. }
  648. size = buf - buffer;
  649. }
  650. return size;
  651. }
  652. /*
  653. * Find the handler for the prefix and dispatch its set() operation.
  654. */
  655. int
  656. generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
  657. {
  658. const struct xattr_handler *handler;
  659. if (size == 0)
  660. value = ""; /* empty EA, do not remove */
  661. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  662. if (!handler)
  663. return -EOPNOTSUPP;
  664. return handler->set(dentry, name, value, size, flags, handler->flags);
  665. }
  666. /*
  667. * Find the handler for the prefix and dispatch its set() operation to remove
  668. * any associated extended attribute.
  669. */
  670. int
  671. generic_removexattr(struct dentry *dentry, const char *name)
  672. {
  673. const struct xattr_handler *handler;
  674. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  675. if (!handler)
  676. return -EOPNOTSUPP;
  677. return handler->set(dentry, name, NULL, 0,
  678. XATTR_REPLACE, handler->flags);
  679. }
  680. EXPORT_SYMBOL(generic_getxattr);
  681. EXPORT_SYMBOL(generic_listxattr);
  682. EXPORT_SYMBOL(generic_setxattr);
  683. EXPORT_SYMBOL(generic_removexattr);