xattr.c 25 KB

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