xattr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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_permission(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. int fput_needed;
  350. struct file *f;
  351. struct dentry *dentry;
  352. int error = -EBADF;
  353. f = fget_light(fd, &fput_needed);
  354. if (!f)
  355. return error;
  356. dentry = f->f_path.dentry;
  357. audit_inode(NULL, dentry);
  358. error = mnt_want_write_file(f);
  359. if (!error) {
  360. error = setxattr(dentry, name, value, size, flags);
  361. mnt_drop_write_file(f);
  362. }
  363. fput_light(f, fput_needed);
  364. return error;
  365. }
  366. /*
  367. * Extended attribute GET operations
  368. */
  369. static ssize_t
  370. getxattr(struct dentry *d, const char __user *name, void __user *value,
  371. size_t size)
  372. {
  373. ssize_t error;
  374. void *kvalue = NULL;
  375. void *vvalue = NULL;
  376. char kname[XATTR_NAME_MAX + 1];
  377. error = strncpy_from_user(kname, name, sizeof(kname));
  378. if (error == 0 || error == sizeof(kname))
  379. error = -ERANGE;
  380. if (error < 0)
  381. return error;
  382. if (size) {
  383. if (size > XATTR_SIZE_MAX)
  384. size = XATTR_SIZE_MAX;
  385. kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  386. if (!kvalue) {
  387. vvalue = vmalloc(size);
  388. if (!vvalue)
  389. return -ENOMEM;
  390. kvalue = vvalue;
  391. }
  392. }
  393. error = vfs_getxattr(d, kname, kvalue, size);
  394. if (error > 0) {
  395. if (size && copy_to_user(value, kvalue, error))
  396. error = -EFAULT;
  397. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  398. /* The file system tried to returned a value bigger
  399. than XATTR_SIZE_MAX bytes. Not possible. */
  400. error = -E2BIG;
  401. }
  402. if (vvalue)
  403. vfree(vvalue);
  404. else
  405. kfree(kvalue);
  406. return error;
  407. }
  408. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  409. const char __user *, name, void __user *, value, size_t, size)
  410. {
  411. struct path path;
  412. ssize_t error;
  413. error = user_path(pathname, &path);
  414. if (error)
  415. return error;
  416. error = getxattr(path.dentry, name, value, size);
  417. path_put(&path);
  418. return error;
  419. }
  420. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  421. const char __user *, name, void __user *, value, size_t, size)
  422. {
  423. struct path path;
  424. ssize_t error;
  425. error = user_lpath(pathname, &path);
  426. if (error)
  427. return error;
  428. error = getxattr(path.dentry, name, value, size);
  429. path_put(&path);
  430. return error;
  431. }
  432. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  433. void __user *, value, size_t, size)
  434. {
  435. int fput_needed;
  436. struct file *f;
  437. ssize_t error = -EBADF;
  438. f = fget_light(fd, &fput_needed);
  439. if (!f)
  440. return error;
  441. audit_inode(NULL, f->f_path.dentry);
  442. error = getxattr(f->f_path.dentry, name, value, size);
  443. fput_light(f, fput_needed);
  444. return error;
  445. }
  446. /*
  447. * Extended attribute LIST operations
  448. */
  449. static ssize_t
  450. listxattr(struct dentry *d, char __user *list, size_t size)
  451. {
  452. ssize_t error;
  453. char *klist = NULL;
  454. char *vlist = NULL; /* If non-NULL, we used vmalloc() */
  455. if (size) {
  456. if (size > XATTR_LIST_MAX)
  457. size = XATTR_LIST_MAX;
  458. klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
  459. if (!klist) {
  460. vlist = vmalloc(size);
  461. if (!vlist)
  462. return -ENOMEM;
  463. klist = vlist;
  464. }
  465. }
  466. error = vfs_listxattr(d, klist, size);
  467. if (error > 0) {
  468. if (size && copy_to_user(list, klist, error))
  469. error = -EFAULT;
  470. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  471. /* The file system tried to returned a list bigger
  472. than XATTR_LIST_MAX bytes. Not possible. */
  473. error = -E2BIG;
  474. }
  475. if (vlist)
  476. vfree(vlist);
  477. else
  478. kfree(klist);
  479. return error;
  480. }
  481. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  482. size_t, size)
  483. {
  484. struct path path;
  485. ssize_t error;
  486. error = user_path(pathname, &path);
  487. if (error)
  488. return error;
  489. error = listxattr(path.dentry, list, size);
  490. path_put(&path);
  491. return error;
  492. }
  493. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  494. size_t, size)
  495. {
  496. struct path path;
  497. ssize_t error;
  498. error = user_lpath(pathname, &path);
  499. if (error)
  500. return error;
  501. error = listxattr(path.dentry, list, size);
  502. path_put(&path);
  503. return error;
  504. }
  505. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  506. {
  507. int fput_needed;
  508. struct file *f;
  509. ssize_t error = -EBADF;
  510. f = fget_light(fd, &fput_needed);
  511. if (!f)
  512. return error;
  513. audit_inode(NULL, f->f_path.dentry);
  514. error = listxattr(f->f_path.dentry, list, size);
  515. fput_light(f, fput_needed);
  516. return error;
  517. }
  518. /*
  519. * Extended attribute REMOVE operations
  520. */
  521. static long
  522. removexattr(struct dentry *d, const char __user *name)
  523. {
  524. int error;
  525. char kname[XATTR_NAME_MAX + 1];
  526. error = strncpy_from_user(kname, name, sizeof(kname));
  527. if (error == 0 || error == sizeof(kname))
  528. error = -ERANGE;
  529. if (error < 0)
  530. return error;
  531. return vfs_removexattr(d, kname);
  532. }
  533. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  534. const char __user *, name)
  535. {
  536. struct path path;
  537. int error;
  538. error = user_path(pathname, &path);
  539. if (error)
  540. return error;
  541. error = mnt_want_write(path.mnt);
  542. if (!error) {
  543. error = removexattr(path.dentry, name);
  544. mnt_drop_write(path.mnt);
  545. }
  546. path_put(&path);
  547. return error;
  548. }
  549. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  550. const char __user *, name)
  551. {
  552. struct path path;
  553. int error;
  554. error = user_lpath(pathname, &path);
  555. if (error)
  556. return error;
  557. error = mnt_want_write(path.mnt);
  558. if (!error) {
  559. error = removexattr(path.dentry, name);
  560. mnt_drop_write(path.mnt);
  561. }
  562. path_put(&path);
  563. return error;
  564. }
  565. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  566. {
  567. int fput_needed;
  568. struct file *f;
  569. struct dentry *dentry;
  570. int error = -EBADF;
  571. f = fget_light(fd, &fput_needed);
  572. if (!f)
  573. return error;
  574. dentry = f->f_path.dentry;
  575. audit_inode(NULL, dentry);
  576. error = mnt_want_write_file(f);
  577. if (!error) {
  578. error = removexattr(dentry, name);
  579. mnt_drop_write_file(f);
  580. }
  581. fput_light(f, fput_needed);
  582. return error;
  583. }
  584. static const char *
  585. strcmp_prefix(const char *a, const char *a_prefix)
  586. {
  587. while (*a_prefix && *a == *a_prefix) {
  588. a++;
  589. a_prefix++;
  590. }
  591. return *a_prefix ? NULL : a;
  592. }
  593. /*
  594. * In order to implement different sets of xattr operations for each xattr
  595. * prefix with the generic xattr API, a filesystem should create a
  596. * null-terminated array of struct xattr_handler (one for each prefix) and
  597. * hang a pointer to it off of the s_xattr field of the superblock.
  598. *
  599. * The generic_fooxattr() functions will use this list to dispatch xattr
  600. * operations to the correct xattr_handler.
  601. */
  602. #define for_each_xattr_handler(handlers, handler) \
  603. for ((handler) = *(handlers)++; \
  604. (handler) != NULL; \
  605. (handler) = *(handlers)++)
  606. /*
  607. * Find the xattr_handler with the matching prefix.
  608. */
  609. static const struct xattr_handler *
  610. xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
  611. {
  612. const struct xattr_handler *handler;
  613. if (!*name)
  614. return NULL;
  615. for_each_xattr_handler(handlers, handler) {
  616. const char *n = strcmp_prefix(*name, handler->prefix);
  617. if (n) {
  618. *name = n;
  619. break;
  620. }
  621. }
  622. return handler;
  623. }
  624. /*
  625. * Find the handler for the prefix and dispatch its get() operation.
  626. */
  627. ssize_t
  628. generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
  629. {
  630. const struct xattr_handler *handler;
  631. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  632. if (!handler)
  633. return -EOPNOTSUPP;
  634. return handler->get(dentry, name, buffer, size, handler->flags);
  635. }
  636. /*
  637. * Combine the results of the list() operation from every xattr_handler in the
  638. * list.
  639. */
  640. ssize_t
  641. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  642. {
  643. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  644. unsigned int size = 0;
  645. if (!buffer) {
  646. for_each_xattr_handler(handlers, handler) {
  647. size += handler->list(dentry, NULL, 0, NULL, 0,
  648. handler->flags);
  649. }
  650. } else {
  651. char *buf = buffer;
  652. for_each_xattr_handler(handlers, handler) {
  653. size = handler->list(dentry, buf, buffer_size,
  654. NULL, 0, handler->flags);
  655. if (size > buffer_size)
  656. return -ERANGE;
  657. buf += size;
  658. buffer_size -= size;
  659. }
  660. size = buf - buffer;
  661. }
  662. return size;
  663. }
  664. /*
  665. * Find the handler for the prefix and dispatch its set() operation.
  666. */
  667. int
  668. generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
  669. {
  670. const struct xattr_handler *handler;
  671. if (size == 0)
  672. value = ""; /* empty EA, do not remove */
  673. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  674. if (!handler)
  675. return -EOPNOTSUPP;
  676. return handler->set(dentry, name, value, size, flags, handler->flags);
  677. }
  678. /*
  679. * Find the handler for the prefix and dispatch its set() operation to remove
  680. * any associated extended attribute.
  681. */
  682. int
  683. generic_removexattr(struct dentry *dentry, const char *name)
  684. {
  685. const struct xattr_handler *handler;
  686. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  687. if (!handler)
  688. return -EOPNOTSUPP;
  689. return handler->set(dentry, name, NULL, 0,
  690. XATTR_REPLACE, handler->flags);
  691. }
  692. EXPORT_SYMBOL(generic_getxattr);
  693. EXPORT_SYMBOL(generic_listxattr);
  694. EXPORT_SYMBOL(generic_setxattr);
  695. EXPORT_SYMBOL(generic_removexattr);