xattr.c 25 KB

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