xattr.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * linux/fs/reiserfs/xattr.c
  3. *
  4. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  5. *
  6. */
  7. /*
  8. * In order to implement EA/ACLs in a clean, backwards compatible manner,
  9. * they are implemented as files in a "private" directory.
  10. * Each EA is in it's own file, with the directory layout like so (/ is assumed
  11. * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
  12. * directories named using the capital-hex form of the objectid and
  13. * generation number are used. Inside each directory are individual files
  14. * named with the name of the extended attribute.
  15. *
  16. * So, for objectid 12648430, we could have:
  17. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
  18. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
  19. * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
  20. * .. or similar.
  21. *
  22. * The file contents are the text of the EA. The size is known based on the
  23. * stat data describing the file.
  24. *
  25. * In the case of system.posix_acl_access and system.posix_acl_default, since
  26. * these are special cases for filesystem ACLs, they are interpreted by the
  27. * kernel, in addition, they are negatively and positively cached and attached
  28. * to the inode so that unnecessary lookups are avoided.
  29. *
  30. * Locking works like so:
  31. * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
  32. * The xattrs themselves are protected by the xattr_sem.
  33. */
  34. #include "reiserfs.h"
  35. #include <linux/capability.h>
  36. #include <linux/dcache.h>
  37. #include <linux/namei.h>
  38. #include <linux/errno.h>
  39. #include <linux/gfp.h>
  40. #include <linux/fs.h>
  41. #include <linux/file.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/xattr.h>
  44. #include "xattr.h"
  45. #include "acl.h"
  46. #include <linux/uaccess.h>
  47. #include <net/checksum.h>
  48. #include <linux/stat.h>
  49. #include <linux/quotaops.h>
  50. #include <linux/security.h>
  51. #include <linux/posix_acl_xattr.h>
  52. #define PRIVROOT_NAME ".reiserfs_priv"
  53. #define XAROOT_NAME "xattrs"
  54. /*
  55. * Helpers for inode ops. We do this so that we don't have all the VFS
  56. * overhead and also for proper i_mutex annotation.
  57. * dir->i_mutex must be held for all of them.
  58. */
  59. #ifdef CONFIG_REISERFS_FS_XATTR
  60. static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
  61. {
  62. BUG_ON(!inode_is_locked(dir));
  63. return dir->i_op->create(dir, dentry, mode, true);
  64. }
  65. #endif
  66. static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  67. {
  68. BUG_ON(!inode_is_locked(dir));
  69. return dir->i_op->mkdir(dir, dentry, mode);
  70. }
  71. /*
  72. * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
  73. * mutation ops aren't called during rename or splace, which are the
  74. * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
  75. * better than allocating another subclass just for this code.
  76. */
  77. static int xattr_unlink(struct inode *dir, struct dentry *dentry)
  78. {
  79. int error;
  80. BUG_ON(!inode_is_locked(dir));
  81. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  82. error = dir->i_op->unlink(dir, dentry);
  83. inode_unlock(d_inode(dentry));
  84. if (!error)
  85. d_delete(dentry);
  86. return error;
  87. }
  88. static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
  89. {
  90. int error;
  91. BUG_ON(!inode_is_locked(dir));
  92. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  93. error = dir->i_op->rmdir(dir, dentry);
  94. if (!error)
  95. d_inode(dentry)->i_flags |= S_DEAD;
  96. inode_unlock(d_inode(dentry));
  97. if (!error)
  98. d_delete(dentry);
  99. return error;
  100. }
  101. #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
  102. static struct dentry *open_xa_root(struct super_block *sb, int flags)
  103. {
  104. struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  105. struct dentry *xaroot;
  106. if (d_really_is_negative(privroot))
  107. return ERR_PTR(-ENODATA);
  108. inode_lock_nested(d_inode(privroot), I_MUTEX_XATTR);
  109. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  110. if (!xaroot)
  111. xaroot = ERR_PTR(-ENODATA);
  112. else if (d_really_is_negative(xaroot)) {
  113. int err = -ENODATA;
  114. if (xattr_may_create(flags))
  115. err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
  116. if (err) {
  117. dput(xaroot);
  118. xaroot = ERR_PTR(err);
  119. }
  120. }
  121. inode_unlock(d_inode(privroot));
  122. return xaroot;
  123. }
  124. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  125. {
  126. struct dentry *xaroot, *xadir;
  127. char namebuf[17];
  128. xaroot = open_xa_root(inode->i_sb, flags);
  129. if (IS_ERR(xaroot))
  130. return xaroot;
  131. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  132. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  133. inode->i_generation);
  134. inode_lock_nested(d_inode(xaroot), I_MUTEX_XATTR);
  135. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  136. if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
  137. int err = -ENODATA;
  138. if (xattr_may_create(flags))
  139. err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
  140. if (err) {
  141. dput(xadir);
  142. xadir = ERR_PTR(err);
  143. }
  144. }
  145. inode_unlock(d_inode(xaroot));
  146. dput(xaroot);
  147. return xadir;
  148. }
  149. /*
  150. * The following are side effects of other operations that aren't explicitly
  151. * modifying extended attributes. This includes operations such as permissions
  152. * or ownership changes, object deletions, etc.
  153. */
  154. struct reiserfs_dentry_buf {
  155. struct dir_context ctx;
  156. struct dentry *xadir;
  157. int count;
  158. struct dentry *dentries[8];
  159. };
  160. static int
  161. fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
  162. loff_t offset, u64 ino, unsigned int d_type)
  163. {
  164. struct reiserfs_dentry_buf *dbuf =
  165. container_of(ctx, struct reiserfs_dentry_buf, ctx);
  166. struct dentry *dentry;
  167. WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
  168. if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
  169. return -ENOSPC;
  170. if (name[0] == '.' && (namelen < 2 ||
  171. (namelen == 2 && name[1] == '.')))
  172. return 0;
  173. dentry = lookup_one_len(name, dbuf->xadir, namelen);
  174. if (IS_ERR(dentry)) {
  175. return PTR_ERR(dentry);
  176. } else if (d_really_is_negative(dentry)) {
  177. /* A directory entry exists, but no file? */
  178. reiserfs_error(dentry->d_sb, "xattr-20003",
  179. "Corrupted directory: xattr %pd listed but "
  180. "not found for file %pd.\n",
  181. dentry, dbuf->xadir);
  182. dput(dentry);
  183. return -EIO;
  184. }
  185. dbuf->dentries[dbuf->count++] = dentry;
  186. return 0;
  187. }
  188. static void
  189. cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
  190. {
  191. int i;
  192. for (i = 0; i < buf->count; i++)
  193. if (buf->dentries[i])
  194. dput(buf->dentries[i]);
  195. }
  196. static int reiserfs_for_each_xattr(struct inode *inode,
  197. int (*action)(struct dentry *, void *),
  198. void *data)
  199. {
  200. struct dentry *dir;
  201. int i, err = 0;
  202. struct reiserfs_dentry_buf buf = {
  203. .ctx.actor = fill_with_dentries,
  204. };
  205. /* Skip out, an xattr has no xattrs associated with it */
  206. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  207. return 0;
  208. dir = open_xa_dir(inode, XATTR_REPLACE);
  209. if (IS_ERR(dir)) {
  210. err = PTR_ERR(dir);
  211. goto out;
  212. } else if (d_really_is_negative(dir)) {
  213. err = 0;
  214. goto out_dir;
  215. }
  216. inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
  217. buf.xadir = dir;
  218. while (1) {
  219. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  220. if (err)
  221. break;
  222. if (!buf.count)
  223. break;
  224. for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
  225. struct dentry *dentry = buf.dentries[i];
  226. if (!d_is_dir(dentry))
  227. err = action(dentry, data);
  228. dput(dentry);
  229. buf.dentries[i] = NULL;
  230. }
  231. if (err)
  232. break;
  233. buf.count = 0;
  234. }
  235. inode_unlock(d_inode(dir));
  236. cleanup_dentry_buf(&buf);
  237. if (!err) {
  238. /*
  239. * We start a transaction here to avoid a ABBA situation
  240. * between the xattr root's i_mutex and the journal lock.
  241. * This doesn't incur much additional overhead since the
  242. * new transaction will just nest inside the
  243. * outer transaction.
  244. */
  245. int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  246. 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  247. struct reiserfs_transaction_handle th;
  248. reiserfs_write_lock(inode->i_sb);
  249. err = journal_begin(&th, inode->i_sb, blocks);
  250. reiserfs_write_unlock(inode->i_sb);
  251. if (!err) {
  252. int jerror;
  253. inode_lock_nested(d_inode(dir->d_parent),
  254. I_MUTEX_XATTR);
  255. err = action(dir, data);
  256. reiserfs_write_lock(inode->i_sb);
  257. jerror = journal_end(&th);
  258. reiserfs_write_unlock(inode->i_sb);
  259. inode_unlock(d_inode(dir->d_parent));
  260. err = jerror ?: err;
  261. }
  262. }
  263. out_dir:
  264. dput(dir);
  265. out:
  266. /* -ENODATA isn't an error */
  267. if (err == -ENODATA)
  268. err = 0;
  269. return err;
  270. }
  271. static int delete_one_xattr(struct dentry *dentry, void *data)
  272. {
  273. struct inode *dir = d_inode(dentry->d_parent);
  274. /* This is the xattr dir, handle specially. */
  275. if (d_is_dir(dentry))
  276. return xattr_rmdir(dir, dentry);
  277. return xattr_unlink(dir, dentry);
  278. }
  279. static int chown_one_xattr(struct dentry *dentry, void *data)
  280. {
  281. struct iattr *attrs = data;
  282. int ia_valid = attrs->ia_valid;
  283. int err;
  284. /*
  285. * We only want the ownership bits. Otherwise, we'll do
  286. * things like change a directory to a regular file if
  287. * ATTR_MODE is set.
  288. */
  289. attrs->ia_valid &= (ATTR_UID|ATTR_GID);
  290. err = reiserfs_setattr(dentry, attrs);
  291. attrs->ia_valid = ia_valid;
  292. return err;
  293. }
  294. /* No i_mutex, but the inode is unconnected. */
  295. int reiserfs_delete_xattrs(struct inode *inode)
  296. {
  297. int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
  298. if (err)
  299. reiserfs_warning(inode->i_sb, "jdm-20004",
  300. "Couldn't delete all xattrs (%d)\n", err);
  301. return err;
  302. }
  303. /* inode->i_mutex: down */
  304. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  305. {
  306. int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
  307. if (err)
  308. reiserfs_warning(inode->i_sb, "jdm-20007",
  309. "Couldn't chown all xattrs (%d)\n", err);
  310. return err;
  311. }
  312. #ifdef CONFIG_REISERFS_FS_XATTR
  313. /*
  314. * Returns a dentry corresponding to a specific extended attribute file
  315. * for the inode. If flags allow, the file is created. Otherwise, a
  316. * valid or negative dentry, or an error is returned.
  317. */
  318. static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  319. int flags)
  320. {
  321. struct dentry *xadir, *xafile;
  322. int err = 0;
  323. xadir = open_xa_dir(inode, flags);
  324. if (IS_ERR(xadir))
  325. return ERR_CAST(xadir);
  326. inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
  327. xafile = lookup_one_len(name, xadir, strlen(name));
  328. if (IS_ERR(xafile)) {
  329. err = PTR_ERR(xafile);
  330. goto out;
  331. }
  332. if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
  333. err = -EEXIST;
  334. if (d_really_is_negative(xafile)) {
  335. err = -ENODATA;
  336. if (xattr_may_create(flags))
  337. err = xattr_create(d_inode(xadir), xafile,
  338. 0700|S_IFREG);
  339. }
  340. if (err)
  341. dput(xafile);
  342. out:
  343. inode_unlock(d_inode(xadir));
  344. dput(xadir);
  345. if (err)
  346. return ERR_PTR(err);
  347. return xafile;
  348. }
  349. /* Internal operations on file data */
  350. static inline void reiserfs_put_page(struct page *page)
  351. {
  352. kunmap(page);
  353. put_page(page);
  354. }
  355. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  356. {
  357. struct address_space *mapping = dir->i_mapping;
  358. struct page *page;
  359. /*
  360. * We can deadlock if we try to free dentries,
  361. * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
  362. */
  363. mapping_set_gfp_mask(mapping, GFP_NOFS);
  364. page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
  365. if (!IS_ERR(page)) {
  366. kmap(page);
  367. if (PageError(page))
  368. goto fail;
  369. }
  370. return page;
  371. fail:
  372. reiserfs_put_page(page);
  373. return ERR_PTR(-EIO);
  374. }
  375. static inline __u32 xattr_hash(const char *msg, int len)
  376. {
  377. return csum_partial(msg, len, 0);
  378. }
  379. int reiserfs_commit_write(struct file *f, struct page *page,
  380. unsigned from, unsigned to);
  381. static void update_ctime(struct inode *inode)
  382. {
  383. struct timespec now = current_time(inode);
  384. if (inode_unhashed(inode) || !inode->i_nlink ||
  385. timespec_equal(&inode->i_ctime, &now))
  386. return;
  387. inode->i_ctime = current_time(inode);
  388. mark_inode_dirty(inode);
  389. }
  390. static int lookup_and_delete_xattr(struct inode *inode, const char *name)
  391. {
  392. int err = 0;
  393. struct dentry *dentry, *xadir;
  394. xadir = open_xa_dir(inode, XATTR_REPLACE);
  395. if (IS_ERR(xadir))
  396. return PTR_ERR(xadir);
  397. inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
  398. dentry = lookup_one_len(name, xadir, strlen(name));
  399. if (IS_ERR(dentry)) {
  400. err = PTR_ERR(dentry);
  401. goto out_dput;
  402. }
  403. if (d_really_is_positive(dentry)) {
  404. err = xattr_unlink(d_inode(xadir), dentry);
  405. update_ctime(inode);
  406. }
  407. dput(dentry);
  408. out_dput:
  409. inode_unlock(d_inode(xadir));
  410. dput(xadir);
  411. return err;
  412. }
  413. /* Generic extended attribute operations that can be used by xa plugins */
  414. /*
  415. * inode->i_mutex: down
  416. */
  417. int
  418. reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  419. struct inode *inode, const char *name,
  420. const void *buffer, size_t buffer_size, int flags)
  421. {
  422. int err = 0;
  423. struct dentry *dentry;
  424. struct page *page;
  425. char *data;
  426. size_t file_pos = 0;
  427. size_t buffer_pos = 0;
  428. size_t new_size;
  429. __u32 xahash = 0;
  430. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  431. return -EOPNOTSUPP;
  432. if (!buffer) {
  433. err = lookup_and_delete_xattr(inode, name);
  434. return err;
  435. }
  436. dentry = xattr_lookup(inode, name, flags);
  437. if (IS_ERR(dentry))
  438. return PTR_ERR(dentry);
  439. down_write(&REISERFS_I(inode)->i_xattr_sem);
  440. xahash = xattr_hash(buffer, buffer_size);
  441. while (buffer_pos < buffer_size || buffer_pos == 0) {
  442. size_t chunk;
  443. size_t skip = 0;
  444. size_t page_offset = (file_pos & (PAGE_SIZE - 1));
  445. if (buffer_size - buffer_pos > PAGE_SIZE)
  446. chunk = PAGE_SIZE;
  447. else
  448. chunk = buffer_size - buffer_pos;
  449. page = reiserfs_get_page(d_inode(dentry), file_pos);
  450. if (IS_ERR(page)) {
  451. err = PTR_ERR(page);
  452. goto out_unlock;
  453. }
  454. lock_page(page);
  455. data = page_address(page);
  456. if (file_pos == 0) {
  457. struct reiserfs_xattr_header *rxh;
  458. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  459. if (chunk + skip > PAGE_SIZE)
  460. chunk = PAGE_SIZE - skip;
  461. rxh = (struct reiserfs_xattr_header *)data;
  462. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  463. rxh->h_hash = cpu_to_le32(xahash);
  464. }
  465. reiserfs_write_lock(inode->i_sb);
  466. err = __reiserfs_write_begin(page, page_offset, chunk + skip);
  467. if (!err) {
  468. if (buffer)
  469. memcpy(data + skip, buffer + buffer_pos, chunk);
  470. err = reiserfs_commit_write(NULL, page, page_offset,
  471. page_offset + chunk +
  472. skip);
  473. }
  474. reiserfs_write_unlock(inode->i_sb);
  475. unlock_page(page);
  476. reiserfs_put_page(page);
  477. buffer_pos += chunk;
  478. file_pos += chunk;
  479. skip = 0;
  480. if (err || buffer_size == 0 || !buffer)
  481. break;
  482. }
  483. new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  484. if (!err && new_size < i_size_read(d_inode(dentry))) {
  485. struct iattr newattrs = {
  486. .ia_ctime = current_time(inode),
  487. .ia_size = new_size,
  488. .ia_valid = ATTR_SIZE | ATTR_CTIME,
  489. };
  490. inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
  491. inode_dio_wait(d_inode(dentry));
  492. err = reiserfs_setattr(dentry, &newattrs);
  493. inode_unlock(d_inode(dentry));
  494. } else
  495. update_ctime(inode);
  496. out_unlock:
  497. up_write(&REISERFS_I(inode)->i_xattr_sem);
  498. dput(dentry);
  499. return err;
  500. }
  501. /* We need to start a transaction to maintain lock ordering */
  502. int reiserfs_xattr_set(struct inode *inode, const char *name,
  503. const void *buffer, size_t buffer_size, int flags)
  504. {
  505. struct reiserfs_transaction_handle th;
  506. int error, error2;
  507. size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  508. if (!(flags & XATTR_REPLACE))
  509. jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  510. reiserfs_write_lock(inode->i_sb);
  511. error = journal_begin(&th, inode->i_sb, jbegin_count);
  512. reiserfs_write_unlock(inode->i_sb);
  513. if (error) {
  514. return error;
  515. }
  516. error = reiserfs_xattr_set_handle(&th, inode, name,
  517. buffer, buffer_size, flags);
  518. reiserfs_write_lock(inode->i_sb);
  519. error2 = journal_end(&th);
  520. reiserfs_write_unlock(inode->i_sb);
  521. if (error == 0)
  522. error = error2;
  523. return error;
  524. }
  525. /*
  526. * inode->i_mutex: down
  527. */
  528. int
  529. reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
  530. size_t buffer_size)
  531. {
  532. ssize_t err = 0;
  533. struct dentry *dentry;
  534. size_t isize;
  535. size_t file_pos = 0;
  536. size_t buffer_pos = 0;
  537. struct page *page;
  538. __u32 hash = 0;
  539. if (name == NULL)
  540. return -EINVAL;
  541. /*
  542. * We can't have xattrs attached to v1 items since they don't have
  543. * generation numbers
  544. */
  545. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  546. return -EOPNOTSUPP;
  547. dentry = xattr_lookup(inode, name, XATTR_REPLACE);
  548. if (IS_ERR(dentry)) {
  549. err = PTR_ERR(dentry);
  550. goto out;
  551. }
  552. down_read(&REISERFS_I(inode)->i_xattr_sem);
  553. isize = i_size_read(d_inode(dentry));
  554. /* Just return the size needed */
  555. if (buffer == NULL) {
  556. err = isize - sizeof(struct reiserfs_xattr_header);
  557. goto out_unlock;
  558. }
  559. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  560. err = -ERANGE;
  561. goto out_unlock;
  562. }
  563. while (file_pos < isize) {
  564. size_t chunk;
  565. char *data;
  566. size_t skip = 0;
  567. if (isize - file_pos > PAGE_SIZE)
  568. chunk = PAGE_SIZE;
  569. else
  570. chunk = isize - file_pos;
  571. page = reiserfs_get_page(d_inode(dentry), file_pos);
  572. if (IS_ERR(page)) {
  573. err = PTR_ERR(page);
  574. goto out_unlock;
  575. }
  576. lock_page(page);
  577. data = page_address(page);
  578. if (file_pos == 0) {
  579. struct reiserfs_xattr_header *rxh =
  580. (struct reiserfs_xattr_header *)data;
  581. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  582. chunk -= skip;
  583. /* Magic doesn't match up.. */
  584. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  585. unlock_page(page);
  586. reiserfs_put_page(page);
  587. reiserfs_warning(inode->i_sb, "jdm-20001",
  588. "Invalid magic for xattr (%s) "
  589. "associated with %k", name,
  590. INODE_PKEY(inode));
  591. err = -EIO;
  592. goto out_unlock;
  593. }
  594. hash = le32_to_cpu(rxh->h_hash);
  595. }
  596. memcpy(buffer + buffer_pos, data + skip, chunk);
  597. unlock_page(page);
  598. reiserfs_put_page(page);
  599. file_pos += chunk;
  600. buffer_pos += chunk;
  601. skip = 0;
  602. }
  603. err = isize - sizeof(struct reiserfs_xattr_header);
  604. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  605. hash) {
  606. reiserfs_warning(inode->i_sb, "jdm-20002",
  607. "Invalid hash for xattr (%s) associated "
  608. "with %k", name, INODE_PKEY(inode));
  609. err = -EIO;
  610. }
  611. out_unlock:
  612. up_read(&REISERFS_I(inode)->i_xattr_sem);
  613. dput(dentry);
  614. out:
  615. return err;
  616. }
  617. /*
  618. * In order to implement different sets of xattr operations for each xattr
  619. * prefix with the generic xattr API, a filesystem should create a
  620. * null-terminated array of struct xattr_handler (one for each prefix) and
  621. * hang a pointer to it off of the s_xattr field of the superblock.
  622. *
  623. * The generic_fooxattr() functions will use this list to dispatch xattr
  624. * operations to the correct xattr_handler.
  625. */
  626. #define for_each_xattr_handler(handlers, handler) \
  627. for ((handler) = *(handlers)++; \
  628. (handler) != NULL; \
  629. (handler) = *(handlers)++)
  630. /* This is the implementation for the xattr plugin infrastructure */
  631. static inline const struct xattr_handler *
  632. find_xattr_handler_prefix(const struct xattr_handler **handlers,
  633. const char *name)
  634. {
  635. const struct xattr_handler *xah;
  636. if (!handlers)
  637. return NULL;
  638. for_each_xattr_handler(handlers, xah) {
  639. const char *prefix = xattr_prefix(xah);
  640. if (strncmp(prefix, name, strlen(prefix)) == 0)
  641. break;
  642. }
  643. return xah;
  644. }
  645. struct listxattr_buf {
  646. struct dir_context ctx;
  647. size_t size;
  648. size_t pos;
  649. char *buf;
  650. struct dentry *dentry;
  651. };
  652. static int listxattr_filler(struct dir_context *ctx, const char *name,
  653. int namelen, loff_t offset, u64 ino,
  654. unsigned int d_type)
  655. {
  656. struct listxattr_buf *b =
  657. container_of(ctx, struct listxattr_buf, ctx);
  658. size_t size;
  659. if (name[0] != '.' ||
  660. (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  661. const struct xattr_handler *handler;
  662. handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
  663. name);
  664. if (!handler /* Unsupported xattr name */ ||
  665. (handler->list && !handler->list(b->dentry)))
  666. return 0;
  667. size = namelen + 1;
  668. if (b->buf) {
  669. if (size > b->size)
  670. return -ERANGE;
  671. memcpy(b->buf + b->pos, name, namelen);
  672. b->buf[b->pos + namelen] = 0;
  673. }
  674. b->pos += size;
  675. }
  676. return 0;
  677. }
  678. /*
  679. * Inode operation listxattr()
  680. *
  681. * We totally ignore the generic listxattr here because it would be stupid
  682. * not to. Since the xattrs are organized in a directory, we can just
  683. * readdir to find them.
  684. */
  685. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  686. {
  687. struct dentry *dir;
  688. int err = 0;
  689. struct listxattr_buf buf = {
  690. .ctx.actor = listxattr_filler,
  691. .dentry = dentry,
  692. .buf = buffer,
  693. .size = buffer ? size : 0,
  694. };
  695. if (d_really_is_negative(dentry))
  696. return -EINVAL;
  697. if (!dentry->d_sb->s_xattr ||
  698. get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
  699. return -EOPNOTSUPP;
  700. dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
  701. if (IS_ERR(dir)) {
  702. err = PTR_ERR(dir);
  703. if (err == -ENODATA)
  704. err = 0; /* Not an error if there aren't any xattrs */
  705. goto out;
  706. }
  707. inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
  708. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  709. inode_unlock(d_inode(dir));
  710. if (!err)
  711. err = buf.pos;
  712. dput(dir);
  713. out:
  714. return err;
  715. }
  716. static int create_privroot(struct dentry *dentry)
  717. {
  718. int err;
  719. struct inode *inode = d_inode(dentry->d_parent);
  720. WARN_ON_ONCE(!inode_is_locked(inode));
  721. err = xattr_mkdir(inode, dentry, 0700);
  722. if (err || d_really_is_negative(dentry)) {
  723. reiserfs_warning(dentry->d_sb, "jdm-20006",
  724. "xattrs/ACLs enabled and couldn't "
  725. "find/create .reiserfs_priv. "
  726. "Failing mount.");
  727. return -EOPNOTSUPP;
  728. }
  729. d_inode(dentry)->i_flags |= S_PRIVATE;
  730. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  731. "storage.\n", PRIVROOT_NAME);
  732. return 0;
  733. }
  734. #else
  735. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  736. void reiserfs_xattr_unregister_handlers(void) {}
  737. static int create_privroot(struct dentry *dentry) { return 0; }
  738. #endif
  739. /* Actual operations that are exported to VFS-land */
  740. static const struct xattr_handler *reiserfs_xattr_handlers[] = {
  741. #ifdef CONFIG_REISERFS_FS_XATTR
  742. &reiserfs_xattr_user_handler,
  743. &reiserfs_xattr_trusted_handler,
  744. #endif
  745. #ifdef CONFIG_REISERFS_FS_SECURITY
  746. &reiserfs_xattr_security_handler,
  747. #endif
  748. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  749. &posix_acl_access_xattr_handler,
  750. &posix_acl_default_xattr_handler,
  751. #endif
  752. NULL
  753. };
  754. static int xattr_mount_check(struct super_block *s)
  755. {
  756. /*
  757. * We need generation numbers to ensure that the oid mapping is correct
  758. * v3.5 filesystems don't have them.
  759. */
  760. if (old_format_only(s)) {
  761. if (reiserfs_xattrs_optional(s)) {
  762. /*
  763. * Old format filesystem, but optional xattrs have
  764. * been enabled. Error out.
  765. */
  766. reiserfs_warning(s, "jdm-2005",
  767. "xattrs/ACLs not supported "
  768. "on pre-v3.6 format filesystems. "
  769. "Failing mount.");
  770. return -EOPNOTSUPP;
  771. }
  772. }
  773. return 0;
  774. }
  775. int reiserfs_permission(struct inode *inode, int mask)
  776. {
  777. /*
  778. * We don't do permission checks on the internal objects.
  779. * Permissions are determined by the "owning" object.
  780. */
  781. if (IS_PRIVATE(inode))
  782. return 0;
  783. return generic_permission(inode, mask);
  784. }
  785. static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
  786. {
  787. return -EPERM;
  788. }
  789. static const struct dentry_operations xattr_lookup_poison_ops = {
  790. .d_revalidate = xattr_hide_revalidate,
  791. };
  792. int reiserfs_lookup_privroot(struct super_block *s)
  793. {
  794. struct dentry *dentry;
  795. int err = 0;
  796. /* If we don't have the privroot located yet - go find it */
  797. inode_lock(d_inode(s->s_root));
  798. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  799. strlen(PRIVROOT_NAME));
  800. if (!IS_ERR(dentry)) {
  801. REISERFS_SB(s)->priv_root = dentry;
  802. d_set_d_op(dentry, &xattr_lookup_poison_ops);
  803. if (d_really_is_positive(dentry))
  804. d_inode(dentry)->i_flags |= S_PRIVATE;
  805. } else
  806. err = PTR_ERR(dentry);
  807. inode_unlock(d_inode(s->s_root));
  808. return err;
  809. }
  810. /*
  811. * We need to take a copy of the mount flags since things like
  812. * MS_RDONLY don't get set until *after* we're called.
  813. * mount_flags != mount_options
  814. */
  815. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  816. {
  817. int err = 0;
  818. struct dentry *privroot = REISERFS_SB(s)->priv_root;
  819. err = xattr_mount_check(s);
  820. if (err)
  821. goto error;
  822. if (d_really_is_negative(privroot) && !(mount_flags & MS_RDONLY)) {
  823. inode_lock(d_inode(s->s_root));
  824. err = create_privroot(REISERFS_SB(s)->priv_root);
  825. inode_unlock(d_inode(s->s_root));
  826. }
  827. if (d_really_is_positive(privroot)) {
  828. s->s_xattr = reiserfs_xattr_handlers;
  829. inode_lock(d_inode(privroot));
  830. if (!REISERFS_SB(s)->xattr_root) {
  831. struct dentry *dentry;
  832. dentry = lookup_one_len(XAROOT_NAME, privroot,
  833. strlen(XAROOT_NAME));
  834. if (!IS_ERR(dentry))
  835. REISERFS_SB(s)->xattr_root = dentry;
  836. else
  837. err = PTR_ERR(dentry);
  838. }
  839. inode_unlock(d_inode(privroot));
  840. }
  841. error:
  842. if (err) {
  843. clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
  844. clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
  845. }
  846. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  847. if (reiserfs_posixacl(s))
  848. s->s_flags |= MS_POSIXACL;
  849. else
  850. s->s_flags &= ~MS_POSIXACL;
  851. return err;
  852. }