xattr.c 18 KB

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