xattr.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/reiserfs/xattr.c
  4. *
  5. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  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(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(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 int
  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 -ENOSPC;
  172. if (name[0] == '.' && (namelen < 2 ||
  173. (namelen == 2 && name[1] == '.')))
  174. return 0;
  175. dentry = lookup_one_len(name, dbuf->xadir, namelen);
  176. if (IS_ERR(dentry)) {
  177. dbuf->err = PTR_ERR(dentry);
  178. return PTR_ERR(dentry);
  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 -EIO;
  188. }
  189. dbuf->dentries[dbuf->count++] = dentry;
  190. return 0;
  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(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. if (PageError(page))
  380. goto fail;
  381. }
  382. return page;
  383. fail:
  384. reiserfs_put_page(page);
  385. return ERR_PTR(-EIO);
  386. }
  387. static inline __u32 xattr_hash(const char *msg, int len)
  388. {
  389. /*
  390. * csum_partial() gives different results for little-endian and
  391. * big endian hosts. Images created on little-endian hosts and
  392. * mounted on big-endian hosts(and vice versa) will see csum mismatches
  393. * when trying to fetch xattrs. Treating the hash as __wsum_t would
  394. * lower the frequency of mismatch. This is an endianness bug in
  395. * reiserfs. The return statement would result in a sparse warning. Do
  396. * not fix the sparse warning so as to not hide a reminder of the bug.
  397. */
  398. return csum_partial(msg, len, 0);
  399. }
  400. int reiserfs_commit_write(struct file *f, struct page *page,
  401. unsigned from, unsigned to);
  402. static void update_ctime(struct inode *inode)
  403. {
  404. struct timespec64 now = current_time(inode);
  405. if (inode_unhashed(inode) || !inode->i_nlink ||
  406. timespec64_equal(&inode->i_ctime, &now))
  407. return;
  408. inode->i_ctime = current_time(inode);
  409. mark_inode_dirty(inode);
  410. }
  411. static int lookup_and_delete_xattr(struct inode *inode, const char *name)
  412. {
  413. int err = 0;
  414. struct dentry *dentry, *xadir;
  415. xadir = open_xa_dir(inode, XATTR_REPLACE);
  416. if (IS_ERR(xadir))
  417. return PTR_ERR(xadir);
  418. inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
  419. dentry = lookup_one_len(name, xadir, strlen(name));
  420. if (IS_ERR(dentry)) {
  421. err = PTR_ERR(dentry);
  422. goto out_dput;
  423. }
  424. if (d_really_is_positive(dentry)) {
  425. err = xattr_unlink(d_inode(xadir), dentry);
  426. update_ctime(inode);
  427. }
  428. dput(dentry);
  429. out_dput:
  430. inode_unlock(d_inode(xadir));
  431. dput(xadir);
  432. return err;
  433. }
  434. /* Generic extended attribute operations that can be used by xa plugins */
  435. /*
  436. * inode->i_mutex: down
  437. */
  438. int
  439. reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  440. struct inode *inode, const char *name,
  441. const void *buffer, size_t buffer_size, int flags)
  442. {
  443. int err = 0;
  444. struct dentry *dentry;
  445. struct page *page;
  446. char *data;
  447. size_t file_pos = 0;
  448. size_t buffer_pos = 0;
  449. size_t new_size;
  450. __u32 xahash = 0;
  451. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  452. return -EOPNOTSUPP;
  453. if (!buffer) {
  454. err = lookup_and_delete_xattr(inode, name);
  455. return err;
  456. }
  457. dentry = xattr_lookup(inode, name, flags);
  458. if (IS_ERR(dentry))
  459. return PTR_ERR(dentry);
  460. down_write(&REISERFS_I(inode)->i_xattr_sem);
  461. xahash = xattr_hash(buffer, buffer_size);
  462. while (buffer_pos < buffer_size || buffer_pos == 0) {
  463. size_t chunk;
  464. size_t skip = 0;
  465. size_t page_offset = (file_pos & (PAGE_SIZE - 1));
  466. if (buffer_size - buffer_pos > PAGE_SIZE)
  467. chunk = PAGE_SIZE;
  468. else
  469. chunk = buffer_size - buffer_pos;
  470. page = reiserfs_get_page(d_inode(dentry), file_pos);
  471. if (IS_ERR(page)) {
  472. err = PTR_ERR(page);
  473. goto out_unlock;
  474. }
  475. lock_page(page);
  476. data = page_address(page);
  477. if (file_pos == 0) {
  478. struct reiserfs_xattr_header *rxh;
  479. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  480. if (chunk + skip > PAGE_SIZE)
  481. chunk = PAGE_SIZE - skip;
  482. rxh = (struct reiserfs_xattr_header *)data;
  483. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  484. rxh->h_hash = cpu_to_le32(xahash);
  485. }
  486. reiserfs_write_lock(inode->i_sb);
  487. err = __reiserfs_write_begin(page, page_offset, chunk + skip);
  488. if (!err) {
  489. if (buffer)
  490. memcpy(data + skip, buffer + buffer_pos, chunk);
  491. err = reiserfs_commit_write(NULL, page, page_offset,
  492. page_offset + chunk +
  493. skip);
  494. }
  495. reiserfs_write_unlock(inode->i_sb);
  496. unlock_page(page);
  497. reiserfs_put_page(page);
  498. buffer_pos += chunk;
  499. file_pos += chunk;
  500. skip = 0;
  501. if (err || buffer_size == 0 || !buffer)
  502. break;
  503. }
  504. new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  505. if (!err && new_size < i_size_read(d_inode(dentry))) {
  506. struct iattr newattrs = {
  507. .ia_ctime = current_time(inode),
  508. .ia_size = new_size,
  509. .ia_valid = ATTR_SIZE | ATTR_CTIME,
  510. };
  511. inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
  512. inode_dio_wait(d_inode(dentry));
  513. err = reiserfs_setattr(dentry, &newattrs);
  514. inode_unlock(d_inode(dentry));
  515. } else
  516. update_ctime(inode);
  517. out_unlock:
  518. up_write(&REISERFS_I(inode)->i_xattr_sem);
  519. dput(dentry);
  520. return err;
  521. }
  522. /* We need to start a transaction to maintain lock ordering */
  523. int reiserfs_xattr_set(struct inode *inode, const char *name,
  524. const void *buffer, size_t buffer_size, int flags)
  525. {
  526. struct reiserfs_transaction_handle th;
  527. int error, error2;
  528. size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  529. /* Check before we start a transaction and then do nothing. */
  530. if (!d_really_is_positive(REISERFS_SB(inode->i_sb)->priv_root))
  531. return -EOPNOTSUPP;
  532. if (!(flags & XATTR_REPLACE))
  533. jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  534. reiserfs_write_lock(inode->i_sb);
  535. error = journal_begin(&th, inode->i_sb, jbegin_count);
  536. reiserfs_write_unlock(inode->i_sb);
  537. if (error) {
  538. return error;
  539. }
  540. error = reiserfs_xattr_set_handle(&th, inode, name,
  541. buffer, buffer_size, flags);
  542. reiserfs_write_lock(inode->i_sb);
  543. error2 = journal_end(&th);
  544. reiserfs_write_unlock(inode->i_sb);
  545. if (error == 0)
  546. error = error2;
  547. return error;
  548. }
  549. /*
  550. * inode->i_mutex: down
  551. */
  552. int
  553. reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
  554. size_t buffer_size)
  555. {
  556. ssize_t err = 0;
  557. struct dentry *dentry;
  558. size_t isize;
  559. size_t file_pos = 0;
  560. size_t buffer_pos = 0;
  561. struct page *page;
  562. __u32 hash = 0;
  563. if (name == NULL)
  564. return -EINVAL;
  565. /*
  566. * We can't have xattrs attached to v1 items since they don't have
  567. * generation numbers
  568. */
  569. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  570. return -EOPNOTSUPP;
  571. /*
  572. * priv_root needn't be initialized during mount so allow initial
  573. * lookups to succeed.
  574. */
  575. if (!REISERFS_SB(inode->i_sb)->priv_root)
  576. return 0;
  577. dentry = xattr_lookup(inode, name, XATTR_REPLACE);
  578. if (IS_ERR(dentry)) {
  579. err = PTR_ERR(dentry);
  580. goto out;
  581. }
  582. down_read(&REISERFS_I(inode)->i_xattr_sem);
  583. isize = i_size_read(d_inode(dentry));
  584. /* Just return the size needed */
  585. if (buffer == NULL) {
  586. err = isize - sizeof(struct reiserfs_xattr_header);
  587. goto out_unlock;
  588. }
  589. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  590. err = -ERANGE;
  591. goto out_unlock;
  592. }
  593. while (file_pos < isize) {
  594. size_t chunk;
  595. char *data;
  596. size_t skip = 0;
  597. if (isize - file_pos > PAGE_SIZE)
  598. chunk = PAGE_SIZE;
  599. else
  600. chunk = isize - file_pos;
  601. page = reiserfs_get_page(d_inode(dentry), file_pos);
  602. if (IS_ERR(page)) {
  603. err = PTR_ERR(page);
  604. goto out_unlock;
  605. }
  606. lock_page(page);
  607. data = page_address(page);
  608. if (file_pos == 0) {
  609. struct reiserfs_xattr_header *rxh =
  610. (struct reiserfs_xattr_header *)data;
  611. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  612. chunk -= skip;
  613. /* Magic doesn't match up.. */
  614. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  615. unlock_page(page);
  616. reiserfs_put_page(page);
  617. reiserfs_warning(inode->i_sb, "jdm-20001",
  618. "Invalid magic for xattr (%s) "
  619. "associated with %k", name,
  620. INODE_PKEY(inode));
  621. err = -EIO;
  622. goto out_unlock;
  623. }
  624. hash = le32_to_cpu(rxh->h_hash);
  625. }
  626. memcpy(buffer + buffer_pos, data + skip, chunk);
  627. unlock_page(page);
  628. reiserfs_put_page(page);
  629. file_pos += chunk;
  630. buffer_pos += chunk;
  631. skip = 0;
  632. }
  633. err = isize - sizeof(struct reiserfs_xattr_header);
  634. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  635. hash) {
  636. reiserfs_warning(inode->i_sb, "jdm-20002",
  637. "Invalid hash for xattr (%s) associated "
  638. "with %k", name, INODE_PKEY(inode));
  639. err = -EIO;
  640. }
  641. out_unlock:
  642. up_read(&REISERFS_I(inode)->i_xattr_sem);
  643. dput(dentry);
  644. out:
  645. return err;
  646. }
  647. /*
  648. * In order to implement different sets of xattr operations for each xattr
  649. * prefix with the generic xattr API, a filesystem should create a
  650. * null-terminated array of struct xattr_handler (one for each prefix) and
  651. * hang a pointer to it off of the s_xattr field of the superblock.
  652. *
  653. * The generic_fooxattr() functions will use this list to dispatch xattr
  654. * operations to the correct xattr_handler.
  655. */
  656. #define for_each_xattr_handler(handlers, handler) \
  657. for ((handler) = *(handlers)++; \
  658. (handler) != NULL; \
  659. (handler) = *(handlers)++)
  660. /* This is the implementation for the xattr plugin infrastructure */
  661. static inline const struct xattr_handler *
  662. find_xattr_handler_prefix(const struct xattr_handler **handlers,
  663. const char *name)
  664. {
  665. const struct xattr_handler *xah;
  666. if (!handlers)
  667. return NULL;
  668. for_each_xattr_handler(handlers, xah) {
  669. const char *prefix = xattr_prefix(xah);
  670. if (strncmp(prefix, name, strlen(prefix)) == 0)
  671. break;
  672. }
  673. return xah;
  674. }
  675. struct listxattr_buf {
  676. struct dir_context ctx;
  677. size_t size;
  678. size_t pos;
  679. char *buf;
  680. struct dentry *dentry;
  681. };
  682. static int listxattr_filler(struct dir_context *ctx, const char *name,
  683. int namelen, loff_t offset, u64 ino,
  684. unsigned int d_type)
  685. {
  686. struct listxattr_buf *b =
  687. container_of(ctx, struct listxattr_buf, ctx);
  688. size_t size;
  689. if (name[0] != '.' ||
  690. (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  691. const struct xattr_handler *handler;
  692. handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
  693. name);
  694. if (!handler /* Unsupported xattr name */ ||
  695. (handler->list && !handler->list(b->dentry)))
  696. return 0;
  697. size = namelen + 1;
  698. if (b->buf) {
  699. if (b->pos + size > b->size) {
  700. b->pos = -ERANGE;
  701. return -ERANGE;
  702. }
  703. memcpy(b->buf + b->pos, name, namelen);
  704. b->buf[b->pos + namelen] = 0;
  705. }
  706. b->pos += size;
  707. }
  708. return 0;
  709. }
  710. /*
  711. * Inode operation listxattr()
  712. *
  713. * We totally ignore the generic listxattr here because it would be stupid
  714. * not to. Since the xattrs are organized in a directory, we can just
  715. * readdir to find them.
  716. */
  717. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  718. {
  719. struct dentry *dir;
  720. int err = 0;
  721. struct listxattr_buf buf = {
  722. .ctx.actor = listxattr_filler,
  723. .dentry = dentry,
  724. .buf = buffer,
  725. .size = buffer ? size : 0,
  726. };
  727. if (d_really_is_negative(dentry))
  728. return -EINVAL;
  729. if (get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
  730. return -EOPNOTSUPP;
  731. dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
  732. if (IS_ERR(dir)) {
  733. err = PTR_ERR(dir);
  734. if (err == -ENODATA)
  735. err = 0; /* Not an error if there aren't any xattrs */
  736. goto out;
  737. }
  738. inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
  739. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  740. inode_unlock(d_inode(dir));
  741. if (!err)
  742. err = buf.pos;
  743. dput(dir);
  744. out:
  745. return err;
  746. }
  747. static int create_privroot(struct dentry *dentry)
  748. {
  749. int err;
  750. struct inode *inode = d_inode(dentry->d_parent);
  751. WARN_ON_ONCE(!inode_is_locked(inode));
  752. err = xattr_mkdir(inode, dentry, 0700);
  753. if (err || d_really_is_negative(dentry)) {
  754. reiserfs_warning(dentry->d_sb, "jdm-20006",
  755. "xattrs/ACLs enabled and couldn't "
  756. "find/create .reiserfs_priv. "
  757. "Failing mount.");
  758. return -EOPNOTSUPP;
  759. }
  760. d_inode(dentry)->i_flags |= S_PRIVATE;
  761. d_inode(dentry)->i_opflags &= ~IOP_XATTR;
  762. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  763. "storage.\n", PRIVROOT_NAME);
  764. return 0;
  765. }
  766. #else
  767. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  768. void reiserfs_xattr_unregister_handlers(void) {}
  769. static int create_privroot(struct dentry *dentry) { return 0; }
  770. #endif
  771. /* Actual operations that are exported to VFS-land */
  772. const struct xattr_handler *reiserfs_xattr_handlers[] = {
  773. #ifdef CONFIG_REISERFS_FS_XATTR
  774. &reiserfs_xattr_user_handler,
  775. &reiserfs_xattr_trusted_handler,
  776. #endif
  777. #ifdef CONFIG_REISERFS_FS_SECURITY
  778. &reiserfs_xattr_security_handler,
  779. #endif
  780. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  781. &posix_acl_access_xattr_handler,
  782. &posix_acl_default_xattr_handler,
  783. #endif
  784. NULL
  785. };
  786. static int xattr_mount_check(struct super_block *s)
  787. {
  788. /*
  789. * We need generation numbers to ensure that the oid mapping is correct
  790. * v3.5 filesystems don't have them.
  791. */
  792. if (old_format_only(s)) {
  793. if (reiserfs_xattrs_optional(s)) {
  794. /*
  795. * Old format filesystem, but optional xattrs have
  796. * been enabled. Error out.
  797. */
  798. reiserfs_warning(s, "jdm-2005",
  799. "xattrs/ACLs not supported "
  800. "on pre-v3.6 format filesystems. "
  801. "Failing mount.");
  802. return -EOPNOTSUPP;
  803. }
  804. }
  805. return 0;
  806. }
  807. int reiserfs_permission(struct inode *inode, int mask)
  808. {
  809. /*
  810. * We don't do permission checks on the internal objects.
  811. * Permissions are determined by the "owning" object.
  812. */
  813. if (IS_PRIVATE(inode))
  814. return 0;
  815. return generic_permission(inode, mask);
  816. }
  817. static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
  818. {
  819. return -EPERM;
  820. }
  821. static const struct dentry_operations xattr_lookup_poison_ops = {
  822. .d_revalidate = xattr_hide_revalidate,
  823. };
  824. int reiserfs_lookup_privroot(struct super_block *s)
  825. {
  826. struct dentry *dentry;
  827. int err = 0;
  828. /* If we don't have the privroot located yet - go find it */
  829. inode_lock(d_inode(s->s_root));
  830. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  831. strlen(PRIVROOT_NAME));
  832. if (!IS_ERR(dentry)) {
  833. REISERFS_SB(s)->priv_root = dentry;
  834. d_set_d_op(dentry, &xattr_lookup_poison_ops);
  835. if (d_really_is_positive(dentry)) {
  836. d_inode(dentry)->i_flags |= S_PRIVATE;
  837. d_inode(dentry)->i_opflags &= ~IOP_XATTR;
  838. }
  839. } else
  840. err = PTR_ERR(dentry);
  841. inode_unlock(d_inode(s->s_root));
  842. return err;
  843. }
  844. /*
  845. * We need to take a copy of the mount flags since things like
  846. * SB_RDONLY don't get set until *after* we're called.
  847. * mount_flags != mount_options
  848. */
  849. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  850. {
  851. int err = 0;
  852. struct dentry *privroot = REISERFS_SB(s)->priv_root;
  853. err = xattr_mount_check(s);
  854. if (err)
  855. goto error;
  856. if (d_really_is_negative(privroot) && !(mount_flags & SB_RDONLY)) {
  857. inode_lock(d_inode(s->s_root));
  858. err = create_privroot(REISERFS_SB(s)->priv_root);
  859. inode_unlock(d_inode(s->s_root));
  860. }
  861. if (d_really_is_positive(privroot)) {
  862. inode_lock(d_inode(privroot));
  863. if (!REISERFS_SB(s)->xattr_root) {
  864. struct dentry *dentry;
  865. dentry = lookup_one_len(XAROOT_NAME, privroot,
  866. strlen(XAROOT_NAME));
  867. if (!IS_ERR(dentry))
  868. REISERFS_SB(s)->xattr_root = dentry;
  869. else
  870. err = PTR_ERR(dentry);
  871. }
  872. inode_unlock(d_inode(privroot));
  873. }
  874. error:
  875. if (err) {
  876. clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
  877. clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
  878. }
  879. /* The super_block SB_POSIXACL must mirror the (no)acl mount option. */
  880. if (reiserfs_posixacl(s))
  881. s->s_flags |= SB_POSIXACL;
  882. else
  883. s->s_flags &= ~SB_POSIXACL;
  884. return err;
  885. }