xattr.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*
  2. * linux/fs/reiserfs/xattr.c
  3. *
  4. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  5. *
  6. */
  7. /*
  8. * In order to implement EA/ACLs in a clean, backwards compatible manner,
  9. * they are implemented as files in a "private" directory.
  10. * Each EA is in it's own file, with the directory layout like so (/ is assumed
  11. * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
  12. * directories named using the capital-hex form of the objectid and
  13. * generation number are used. Inside each directory are individual files
  14. * named with the name of the extended attribute.
  15. *
  16. * So, for objectid 12648430, we could have:
  17. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
  18. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
  19. * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
  20. * .. or similar.
  21. *
  22. * The file contents are the text of the EA. The size is known based on the
  23. * stat data describing the file.
  24. *
  25. * In the case of system.posix_acl_access and system.posix_acl_default, since
  26. * these are special cases for filesystem ACLs, they are interpreted by the
  27. * kernel, in addition, they are negatively and positively cached and attached
  28. * to the inode so that unnecessary lookups are avoided.
  29. */
  30. #include <linux/reiserfs_fs.h>
  31. #include <linux/capability.h>
  32. #include <linux/dcache.h>
  33. #include <linux/namei.h>
  34. #include <linux/errno.h>
  35. #include <linux/fs.h>
  36. #include <linux/file.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/xattr.h>
  39. #include <linux/reiserfs_xattr.h>
  40. #include <linux/reiserfs_acl.h>
  41. #include <asm/uaccess.h>
  42. #include <net/checksum.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/stat.h>
  45. #define FL_READONLY 128
  46. #define FL_DIR_SEM_HELD 256
  47. #define PRIVROOT_NAME ".reiserfs_priv"
  48. #define XAROOT_NAME "xattrs"
  49. /* Returns the dentry referring to the root of the extended attribute
  50. * directory tree. If it has already been retrieved, it is used. If it
  51. * hasn't been created and the flags indicate creation is allowed, we
  52. * attempt to create it. On error, we return a pointer-encoded error.
  53. */
  54. static struct dentry *get_xa_root(struct super_block *sb, int flags)
  55. {
  56. struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
  57. struct dentry *xaroot;
  58. /* This needs to be created at mount-time */
  59. if (!privroot)
  60. return ERR_PTR(-ENODATA);
  61. mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
  62. if (REISERFS_SB(sb)->xattr_root) {
  63. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  64. goto out;
  65. }
  66. xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
  67. if (IS_ERR(xaroot)) {
  68. goto out;
  69. } else if (!xaroot->d_inode) {
  70. int err = -ENODATA;
  71. if (flags == 0 || flags & XATTR_CREATE)
  72. err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
  73. xaroot, 0700);
  74. if (err) {
  75. dput(xaroot);
  76. xaroot = ERR_PTR(err);
  77. goto out;
  78. }
  79. }
  80. REISERFS_SB(sb)->xattr_root = dget(xaroot);
  81. out:
  82. mutex_unlock(&privroot->d_inode->i_mutex);
  83. dput(privroot);
  84. return xaroot;
  85. }
  86. /* Opens the directory corresponding to the inode's extended attribute store.
  87. * If flags allow, the tree to the directory may be created. If creation is
  88. * prohibited, -ENODATA is returned. */
  89. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  90. {
  91. struct dentry *xaroot, *xadir;
  92. char namebuf[17];
  93. xaroot = get_xa_root(inode->i_sb, flags);
  94. if (IS_ERR(xaroot))
  95. return xaroot;
  96. /* ok, we have xaroot open */
  97. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  98. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  99. inode->i_generation);
  100. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  101. if (IS_ERR(xadir)) {
  102. dput(xaroot);
  103. return xadir;
  104. }
  105. if (!xadir->d_inode) {
  106. int err;
  107. if (flags == 0 || flags & XATTR_CREATE) {
  108. /* Although there is nothing else trying to create this directory,
  109. * another directory with the same hash may be created, so we need
  110. * to protect against that */
  111. err =
  112. xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
  113. 0700);
  114. if (err) {
  115. dput(xaroot);
  116. dput(xadir);
  117. return ERR_PTR(err);
  118. }
  119. }
  120. if (!xadir->d_inode) {
  121. dput(xaroot);
  122. dput(xadir);
  123. return ERR_PTR(-ENODATA);
  124. }
  125. }
  126. dput(xaroot);
  127. return xadir;
  128. }
  129. /*
  130. * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
  131. * we need to drop the path before calling the filldir struct. That
  132. * would be a big performance hit to the non-xattr case, so I've copied
  133. * the whole thing for now. --clm
  134. *
  135. * the big difference is that I go backwards through the directory,
  136. * and don't mess with f->f_pos, but the idea is the same. Do some
  137. * action on each and every entry in the directory.
  138. *
  139. * we're called with i_mutex held, so there are no worries about the directory
  140. * changing underneath us.
  141. */
  142. static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
  143. {
  144. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  145. INITIALIZE_PATH(path_to_entry);
  146. struct buffer_head *bh;
  147. int entry_num;
  148. struct item_head *ih, tmp_ih;
  149. int search_res;
  150. char *local_buf;
  151. loff_t next_pos;
  152. char small_buf[32]; /* avoid kmalloc if we can */
  153. struct reiserfs_de_head *deh;
  154. int d_reclen;
  155. char *d_name;
  156. off_t d_off;
  157. ino_t d_ino;
  158. struct reiserfs_dir_entry de;
  159. /* form key for search the next directory entry using f_pos field of
  160. file structure */
  161. next_pos = max_reiserfs_offset(inode);
  162. while (1) {
  163. research:
  164. if (next_pos <= DOT_DOT_OFFSET)
  165. break;
  166. make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
  167. search_res =
  168. search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  169. &de);
  170. if (search_res == IO_ERROR) {
  171. // FIXME: we could just skip part of directory which could
  172. // not be read
  173. pathrelse(&path_to_entry);
  174. return -EIO;
  175. }
  176. if (search_res == NAME_NOT_FOUND)
  177. de.de_entry_num--;
  178. set_de_name_and_namelen(&de);
  179. entry_num = de.de_entry_num;
  180. deh = &(de.de_deh[entry_num]);
  181. bh = de.de_bh;
  182. ih = de.de_ih;
  183. if (!is_direntry_le_ih(ih)) {
  184. reiserfs_error(inode->i_sb, "jdm-20000",
  185. "not direntry %h", ih);
  186. break;
  187. }
  188. copy_item_head(&tmp_ih, ih);
  189. /* we must have found item, that is item of this directory, */
  190. RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  191. "vs-9000: found item %h does not match to dir we readdir %K",
  192. ih, &pos_key);
  193. if (deh_offset(deh) <= DOT_DOT_OFFSET) {
  194. break;
  195. }
  196. /* look for the previous entry in the directory */
  197. next_pos = deh_offset(deh) - 1;
  198. if (!de_visible(deh))
  199. /* it is hidden entry */
  200. continue;
  201. d_reclen = entry_length(bh, ih, entry_num);
  202. d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
  203. d_off = deh_offset(deh);
  204. d_ino = deh_objectid(deh);
  205. if (!d_name[d_reclen - 1])
  206. d_reclen = strlen(d_name);
  207. if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
  208. /* too big to send back to VFS */
  209. continue;
  210. }
  211. /* Ignore the .reiserfs_priv entry */
  212. if (reiserfs_xattrs(inode->i_sb) &&
  213. !old_format_only(inode->i_sb) &&
  214. deh_objectid(deh) ==
  215. le32_to_cpu(INODE_PKEY
  216. (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
  217. k_objectid))
  218. continue;
  219. if (d_reclen <= 32) {
  220. local_buf = small_buf;
  221. } else {
  222. local_buf = kmalloc(d_reclen, GFP_NOFS);
  223. if (!local_buf) {
  224. pathrelse(&path_to_entry);
  225. return -ENOMEM;
  226. }
  227. if (item_moved(&tmp_ih, &path_to_entry)) {
  228. kfree(local_buf);
  229. /* sigh, must retry. Do this same offset again */
  230. next_pos = d_off;
  231. goto research;
  232. }
  233. }
  234. // Note, that we copy name to user space via temporary
  235. // buffer (local_buf) because filldir will block if
  236. // user space buffer is swapped out. At that time
  237. // entry can move to somewhere else
  238. memcpy(local_buf, d_name, d_reclen);
  239. /* the filldir function might need to start transactions,
  240. * or do who knows what. Release the path now that we've
  241. * copied all the important stuff out of the deh
  242. */
  243. pathrelse(&path_to_entry);
  244. if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
  245. DT_UNKNOWN) < 0) {
  246. if (local_buf != small_buf) {
  247. kfree(local_buf);
  248. }
  249. goto end;
  250. }
  251. if (local_buf != small_buf) {
  252. kfree(local_buf);
  253. }
  254. } /* while */
  255. end:
  256. pathrelse(&path_to_entry);
  257. return 0;
  258. }
  259. /*
  260. * this could be done with dedicated readdir ops for the xattr files,
  261. * but I want to get something working asap
  262. * this is stolen from vfs_readdir
  263. *
  264. */
  265. static
  266. int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
  267. {
  268. int res = -ENOENT;
  269. mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
  270. if (!IS_DEADDIR(inode)) {
  271. lock_kernel();
  272. res = __xattr_readdir(inode, buf, filler);
  273. unlock_kernel();
  274. }
  275. mutex_unlock(&inode->i_mutex);
  276. return res;
  277. }
  278. static int
  279. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  280. {
  281. struct dentry *dentry;
  282. struct inode *dir = xadir->d_inode;
  283. int err = 0;
  284. dentry = lookup_one_len(name, xadir, namelen);
  285. if (IS_ERR(dentry)) {
  286. err = PTR_ERR(dentry);
  287. goto out;
  288. } else if (!dentry->d_inode) {
  289. err = -ENODATA;
  290. goto out_file;
  291. }
  292. /* Skip directories.. */
  293. if (S_ISDIR(dentry->d_inode->i_mode))
  294. goto out_file;
  295. if (!IS_PRIVATE(dentry->d_inode)) {
  296. reiserfs_error(dir->i_sb, "jdm-20003",
  297. "OID %08x [%.*s/%.*s] doesn't have "
  298. "priv flag set [parent is %sset].",
  299. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  300. k_objectid), xadir->d_name.len,
  301. xadir->d_name.name, namelen, name,
  302. IS_PRIVATE(xadir->d_inode) ? "" :
  303. "not ");
  304. dput(dentry);
  305. return -EIO;
  306. }
  307. err = dir->i_op->unlink(dir, dentry);
  308. if (!err)
  309. d_delete(dentry);
  310. out_file:
  311. dput(dentry);
  312. out:
  313. return err;
  314. }
  315. /* The following are side effects of other operations that aren't explicitly
  316. * modifying extended attributes. This includes operations such as permissions
  317. * or ownership changes, object deletions, etc. */
  318. static int
  319. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  320. loff_t offset, u64 ino, unsigned int d_type)
  321. {
  322. struct dentry *xadir = (struct dentry *)buf;
  323. return __reiserfs_xattr_del(xadir, name, namelen);
  324. }
  325. /* This is called w/ inode->i_mutex downed */
  326. int reiserfs_delete_xattrs(struct inode *inode)
  327. {
  328. struct dentry *dir, *root;
  329. int err = 0;
  330. /* Skip out, an xattr has no xattrs associated with it */
  331. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  332. return 0;
  333. reiserfs_read_lock_xattrs(inode->i_sb);
  334. dir = open_xa_dir(inode, FL_READONLY);
  335. reiserfs_read_unlock_xattrs(inode->i_sb);
  336. if (IS_ERR(dir)) {
  337. err = PTR_ERR(dir);
  338. goto out;
  339. } else if (!dir->d_inode) {
  340. dput(dir);
  341. return 0;
  342. }
  343. lock_kernel();
  344. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  345. if (err) {
  346. unlock_kernel();
  347. goto out_dir;
  348. }
  349. /* Leftovers besides . and .. -- that's not good. */
  350. if (dir->d_inode->i_nlink <= 2) {
  351. root = get_xa_root(inode->i_sb, XATTR_REPLACE);
  352. reiserfs_write_lock_xattrs(inode->i_sb);
  353. err = vfs_rmdir(root->d_inode, dir);
  354. reiserfs_write_unlock_xattrs(inode->i_sb);
  355. dput(root);
  356. } else {
  357. reiserfs_warning(inode->i_sb, "jdm-20006",
  358. "Couldn't remove all entries in directory");
  359. }
  360. unlock_kernel();
  361. out_dir:
  362. dput(dir);
  363. out:
  364. if (!err)
  365. REISERFS_I(inode)->i_flags =
  366. REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  367. return err;
  368. }
  369. struct reiserfs_chown_buf {
  370. struct inode *inode;
  371. struct dentry *xadir;
  372. struct iattr *attrs;
  373. };
  374. /* XXX: If there is a better way to do this, I'd love to hear about it */
  375. static int
  376. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  377. loff_t offset, u64 ino, unsigned int d_type)
  378. {
  379. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  380. struct dentry *xafile, *xadir = chown_buf->xadir;
  381. struct iattr *attrs = chown_buf->attrs;
  382. int err = 0;
  383. xafile = lookup_one_len(name, xadir, namelen);
  384. if (IS_ERR(xafile))
  385. return PTR_ERR(xafile);
  386. else if (!xafile->d_inode) {
  387. dput(xafile);
  388. return -ENODATA;
  389. }
  390. if (!S_ISDIR(xafile->d_inode->i_mode))
  391. err = notify_change(xafile, attrs);
  392. dput(xafile);
  393. return err;
  394. }
  395. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  396. {
  397. struct dentry *dir;
  398. int err = 0;
  399. struct reiserfs_chown_buf buf;
  400. unsigned int ia_valid = attrs->ia_valid;
  401. /* Skip out, an xattr has no xattrs associated with it */
  402. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  403. return 0;
  404. reiserfs_read_lock_xattrs(inode->i_sb);
  405. dir = open_xa_dir(inode, FL_READONLY);
  406. reiserfs_read_unlock_xattrs(inode->i_sb);
  407. if (IS_ERR(dir)) {
  408. if (PTR_ERR(dir) != -ENODATA)
  409. err = PTR_ERR(dir);
  410. goto out;
  411. } else if (!dir->d_inode) {
  412. dput(dir);
  413. goto out;
  414. }
  415. lock_kernel();
  416. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  417. buf.xadir = dir;
  418. buf.attrs = attrs;
  419. buf.inode = inode;
  420. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  421. if (err) {
  422. unlock_kernel();
  423. goto out_dir;
  424. }
  425. err = notify_change(dir, attrs);
  426. unlock_kernel();
  427. out_dir:
  428. dput(dir);
  429. out:
  430. attrs->ia_valid = ia_valid;
  431. return err;
  432. }
  433. #ifdef CONFIG_REISERFS_FS_XATTR
  434. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  435. *prefix);
  436. /* Returns a dentry corresponding to a specific extended attribute file
  437. * for the inode. If flags allow, the file is created. Otherwise, a
  438. * valid or negative dentry, or an error is returned. */
  439. static struct dentry *get_xa_file_dentry(const struct inode *inode,
  440. const char *name, int flags)
  441. {
  442. struct dentry *xadir, *xafile;
  443. int err = 0;
  444. xadir = open_xa_dir(inode, flags);
  445. if (IS_ERR(xadir)) {
  446. return ERR_CAST(xadir);
  447. } else if (xadir && !xadir->d_inode) {
  448. dput(xadir);
  449. return ERR_PTR(-ENODATA);
  450. }
  451. xafile = lookup_one_len(name, xadir, strlen(name));
  452. if (IS_ERR(xafile)) {
  453. dput(xadir);
  454. return ERR_CAST(xafile);
  455. }
  456. if (xafile->d_inode) { /* file exists */
  457. if (flags & XATTR_CREATE) {
  458. err = -EEXIST;
  459. dput(xafile);
  460. goto out;
  461. }
  462. } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
  463. goto out;
  464. } else {
  465. /* inode->i_mutex is down, so nothing else can try to create
  466. * the same xattr */
  467. err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
  468. 0700 | S_IFREG, NULL);
  469. if (err) {
  470. dput(xafile);
  471. goto out;
  472. }
  473. }
  474. out:
  475. dput(xadir);
  476. if (err)
  477. xafile = ERR_PTR(err);
  478. else if (!xafile->d_inode) {
  479. dput(xafile);
  480. xafile = ERR_PTR(-ENODATA);
  481. }
  482. return xafile;
  483. }
  484. /* Internal operations on file data */
  485. static inline void reiserfs_put_page(struct page *page)
  486. {
  487. kunmap(page);
  488. page_cache_release(page);
  489. }
  490. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  491. {
  492. struct address_space *mapping = dir->i_mapping;
  493. struct page *page;
  494. /* We can deadlock if we try to free dentries,
  495. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  496. mapping_set_gfp_mask(mapping, GFP_NOFS);
  497. page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
  498. if (!IS_ERR(page)) {
  499. kmap(page);
  500. if (PageError(page))
  501. goto fail;
  502. }
  503. return page;
  504. fail:
  505. reiserfs_put_page(page);
  506. return ERR_PTR(-EIO);
  507. }
  508. static inline __u32 xattr_hash(const char *msg, int len)
  509. {
  510. return csum_partial(msg, len, 0);
  511. }
  512. int reiserfs_commit_write(struct file *f, struct page *page,
  513. unsigned from, unsigned to);
  514. int reiserfs_prepare_write(struct file *f, struct page *page,
  515. unsigned from, unsigned to);
  516. /* Generic extended attribute operations that can be used by xa plugins */
  517. /*
  518. * inode->i_mutex: down
  519. */
  520. int
  521. reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
  522. size_t buffer_size, int flags)
  523. {
  524. int err = 0;
  525. struct dentry *dentry;
  526. struct page *page;
  527. char *data;
  528. size_t file_pos = 0;
  529. size_t buffer_pos = 0;
  530. struct iattr newattrs;
  531. __u32 xahash = 0;
  532. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  533. return -EOPNOTSUPP;
  534. /* Empty xattrs are ok, they're just empty files, no hash */
  535. if (buffer && buffer_size)
  536. xahash = xattr_hash(buffer, buffer_size);
  537. dentry = get_xa_file_dentry(inode, name, flags);
  538. if (IS_ERR(dentry)) {
  539. err = PTR_ERR(dentry);
  540. goto out;
  541. }
  542. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  543. /* Resize it so we're ok to write there */
  544. newattrs.ia_size = buffer_size;
  545. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  546. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
  547. err = notify_change(dentry, &newattrs);
  548. if (err)
  549. goto out_filp;
  550. while (buffer_pos < buffer_size || buffer_pos == 0) {
  551. size_t chunk;
  552. size_t skip = 0;
  553. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  554. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  555. chunk = PAGE_CACHE_SIZE;
  556. else
  557. chunk = buffer_size - buffer_pos;
  558. page = reiserfs_get_page(dentry->d_inode, file_pos);
  559. if (IS_ERR(page)) {
  560. err = PTR_ERR(page);
  561. goto out_filp;
  562. }
  563. lock_page(page);
  564. data = page_address(page);
  565. if (file_pos == 0) {
  566. struct reiserfs_xattr_header *rxh;
  567. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  568. if (chunk + skip > PAGE_CACHE_SIZE)
  569. chunk = PAGE_CACHE_SIZE - skip;
  570. rxh = (struct reiserfs_xattr_header *)data;
  571. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  572. rxh->h_hash = cpu_to_le32(xahash);
  573. }
  574. err = reiserfs_prepare_write(NULL, page, page_offset,
  575. page_offset + chunk + skip);
  576. if (!err) {
  577. if (buffer)
  578. memcpy(data + skip, buffer + buffer_pos, chunk);
  579. err = reiserfs_commit_write(NULL, page, page_offset,
  580. page_offset + chunk +
  581. skip);
  582. }
  583. unlock_page(page);
  584. reiserfs_put_page(page);
  585. buffer_pos += chunk;
  586. file_pos += chunk;
  587. skip = 0;
  588. if (err || buffer_size == 0 || !buffer)
  589. break;
  590. }
  591. /* We can't mark the inode dirty if it's not hashed. This is the case
  592. * when we're inheriting the default ACL. If we dirty it, the inode
  593. * gets marked dirty, but won't (ever) make it onto the dirty list until
  594. * it's synced explicitly to clear I_DIRTY. This is bad. */
  595. if (!hlist_unhashed(&inode->i_hash)) {
  596. inode->i_ctime = CURRENT_TIME_SEC;
  597. mark_inode_dirty(inode);
  598. }
  599. out_filp:
  600. mutex_unlock(&dentry->d_inode->i_mutex);
  601. dput(dentry);
  602. out:
  603. return err;
  604. }
  605. /*
  606. * inode->i_mutex: down
  607. */
  608. int
  609. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  610. size_t buffer_size)
  611. {
  612. ssize_t err = 0;
  613. struct dentry *dentry;
  614. size_t isize;
  615. size_t file_pos = 0;
  616. size_t buffer_pos = 0;
  617. struct page *page;
  618. __u32 hash = 0;
  619. if (name == NULL)
  620. return -EINVAL;
  621. /* We can't have xattrs attached to v1 items since they don't have
  622. * generation numbers */
  623. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  624. return -EOPNOTSUPP;
  625. dentry = get_xa_file_dentry(inode, name, FL_READONLY);
  626. if (IS_ERR(dentry)) {
  627. err = PTR_ERR(dentry);
  628. goto out;
  629. }
  630. isize = i_size_read(dentry->d_inode);
  631. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  632. /* Just return the size needed */
  633. if (buffer == NULL) {
  634. err = isize - sizeof(struct reiserfs_xattr_header);
  635. goto out_dput;
  636. }
  637. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  638. err = -ERANGE;
  639. goto out_dput;
  640. }
  641. while (file_pos < isize) {
  642. size_t chunk;
  643. char *data;
  644. size_t skip = 0;
  645. if (isize - file_pos > PAGE_CACHE_SIZE)
  646. chunk = PAGE_CACHE_SIZE;
  647. else
  648. chunk = isize - file_pos;
  649. page = reiserfs_get_page(dentry->d_inode, file_pos);
  650. if (IS_ERR(page)) {
  651. err = PTR_ERR(page);
  652. goto out_dput;
  653. }
  654. lock_page(page);
  655. data = page_address(page);
  656. if (file_pos == 0) {
  657. struct reiserfs_xattr_header *rxh =
  658. (struct reiserfs_xattr_header *)data;
  659. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  660. chunk -= skip;
  661. /* Magic doesn't match up.. */
  662. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  663. unlock_page(page);
  664. reiserfs_put_page(page);
  665. reiserfs_warning(inode->i_sb, "jdm-20001",
  666. "Invalid magic for xattr (%s) "
  667. "associated with %k", name,
  668. INODE_PKEY(inode));
  669. err = -EIO;
  670. goto out_dput;
  671. }
  672. hash = le32_to_cpu(rxh->h_hash);
  673. }
  674. memcpy(buffer + buffer_pos, data + skip, chunk);
  675. unlock_page(page);
  676. reiserfs_put_page(page);
  677. file_pos += chunk;
  678. buffer_pos += chunk;
  679. skip = 0;
  680. }
  681. err = isize - sizeof(struct reiserfs_xattr_header);
  682. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  683. hash) {
  684. reiserfs_warning(inode->i_sb, "jdm-20002",
  685. "Invalid hash for xattr (%s) associated "
  686. "with %k", name, INODE_PKEY(inode));
  687. err = -EIO;
  688. }
  689. out_dput:
  690. dput(dentry);
  691. out:
  692. return err;
  693. }
  694. int reiserfs_xattr_del(struct inode *inode, const char *name)
  695. {
  696. struct dentry *dir;
  697. int err;
  698. dir = open_xa_dir(inode, FL_READONLY);
  699. if (IS_ERR(dir)) {
  700. err = PTR_ERR(dir);
  701. goto out;
  702. }
  703. err = __reiserfs_xattr_del(dir, name, strlen(name));
  704. dput(dir);
  705. if (!err) {
  706. inode->i_ctime = CURRENT_TIME_SEC;
  707. mark_inode_dirty(inode);
  708. }
  709. out:
  710. return err;
  711. }
  712. /* Actual operations that are exported to VFS-land */
  713. /*
  714. * Inode operation getxattr()
  715. * Preliminary locking: we down dentry->d_inode->i_mutex
  716. */
  717. ssize_t
  718. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  719. size_t size)
  720. {
  721. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  722. int err;
  723. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  724. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  725. return -EOPNOTSUPP;
  726. reiserfs_read_lock_xattr_i(dentry->d_inode);
  727. reiserfs_read_lock_xattrs(dentry->d_sb);
  728. err = xah->get(dentry->d_inode, name, buffer, size);
  729. reiserfs_read_unlock_xattrs(dentry->d_sb);
  730. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  731. return err;
  732. }
  733. /*
  734. * Inode operation setxattr()
  735. *
  736. * dentry->d_inode->i_mutex down
  737. */
  738. int
  739. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  740. size_t size, int flags)
  741. {
  742. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  743. int err;
  744. int lock;
  745. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  746. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  747. return -EOPNOTSUPP;
  748. reiserfs_write_lock_xattr_i(dentry->d_inode);
  749. lock = !has_xattr_dir(dentry->d_inode);
  750. if (lock)
  751. reiserfs_write_lock_xattrs(dentry->d_sb);
  752. else
  753. reiserfs_read_lock_xattrs(dentry->d_sb);
  754. err = xah->set(dentry->d_inode, name, value, size, flags);
  755. if (lock)
  756. reiserfs_write_unlock_xattrs(dentry->d_sb);
  757. else
  758. reiserfs_read_unlock_xattrs(dentry->d_sb);
  759. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  760. return err;
  761. }
  762. /*
  763. * Inode operation removexattr()
  764. *
  765. * dentry->d_inode->i_mutex down
  766. */
  767. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  768. {
  769. int err;
  770. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  771. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  772. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  773. return -EOPNOTSUPP;
  774. reiserfs_write_lock_xattr_i(dentry->d_inode);
  775. reiserfs_read_lock_xattrs(dentry->d_sb);
  776. /* Deletion pre-operation */
  777. if (xah->del) {
  778. err = xah->del(dentry->d_inode, name);
  779. if (err)
  780. goto out;
  781. }
  782. err = reiserfs_xattr_del(dentry->d_inode, name);
  783. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  784. mark_inode_dirty(dentry->d_inode);
  785. out:
  786. reiserfs_read_unlock_xattrs(dentry->d_sb);
  787. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  788. return err;
  789. }
  790. /* This is what filldir will use:
  791. * r_pos will always contain the amount of space required for the entire
  792. * list. If r_pos becomes larger than r_size, we need more space and we
  793. * return an error indicating this. If r_pos is less than r_size, then we've
  794. * filled the buffer successfully and we return success */
  795. struct reiserfs_listxattr_buf {
  796. int r_pos;
  797. int r_size;
  798. char *r_buf;
  799. struct inode *r_inode;
  800. };
  801. static int
  802. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  803. loff_t offset, u64 ino, unsigned int d_type)
  804. {
  805. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  806. int len = 0;
  807. if (name[0] != '.'
  808. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  809. struct reiserfs_xattr_handler *xah =
  810. find_xattr_handler_prefix(name);
  811. if (!xah)
  812. return 0; /* Unsupported xattr name, skip it */
  813. /* We call ->list() twice because the operation isn't required to just
  814. * return the name back - we want to make sure we have enough space */
  815. len += xah->list(b->r_inode, name, namelen, NULL);
  816. if (len) {
  817. if (b->r_pos + len + 1 <= b->r_size) {
  818. char *p = b->r_buf + b->r_pos;
  819. p += xah->list(b->r_inode, name, namelen, p);
  820. *p++ = '\0';
  821. }
  822. b->r_pos += len + 1;
  823. }
  824. }
  825. return 0;
  826. }
  827. /*
  828. * Inode operation listxattr()
  829. *
  830. * Preliminary locking: we down dentry->d_inode->i_mutex
  831. */
  832. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  833. {
  834. struct dentry *dir;
  835. int err = 0;
  836. struct reiserfs_listxattr_buf buf;
  837. if (!dentry->d_inode)
  838. return -EINVAL;
  839. if (!reiserfs_xattrs(dentry->d_sb) ||
  840. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  841. return -EOPNOTSUPP;
  842. reiserfs_read_lock_xattr_i(dentry->d_inode);
  843. reiserfs_read_lock_xattrs(dentry->d_sb);
  844. dir = open_xa_dir(dentry->d_inode, FL_READONLY);
  845. reiserfs_read_unlock_xattrs(dentry->d_sb);
  846. if (IS_ERR(dir)) {
  847. err = PTR_ERR(dir);
  848. if (err == -ENODATA)
  849. err = 0; /* Not an error if there aren't any xattrs */
  850. goto out;
  851. }
  852. buf.r_buf = buffer;
  853. buf.r_size = buffer ? size : 0;
  854. buf.r_pos = 0;
  855. buf.r_inode = dentry->d_inode;
  856. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  857. err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
  858. if (err)
  859. goto out_dir;
  860. if (buf.r_pos > buf.r_size && buffer != NULL)
  861. err = -ERANGE;
  862. else
  863. err = buf.r_pos;
  864. out_dir:
  865. dput(dir);
  866. out:
  867. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  868. return err;
  869. }
  870. /* This is the implementation for the xattr plugin infrastructure */
  871. static LIST_HEAD(xattr_handlers);
  872. static DEFINE_RWLOCK(handler_lock);
  873. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  874. *prefix)
  875. {
  876. struct reiserfs_xattr_handler *xah = NULL;
  877. struct list_head *p;
  878. read_lock(&handler_lock);
  879. list_for_each(p, &xattr_handlers) {
  880. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  881. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  882. break;
  883. xah = NULL;
  884. }
  885. read_unlock(&handler_lock);
  886. return xah;
  887. }
  888. static void __unregister_handlers(void)
  889. {
  890. struct reiserfs_xattr_handler *xah;
  891. struct list_head *p, *tmp;
  892. list_for_each_safe(p, tmp, &xattr_handlers) {
  893. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  894. if (xah->exit)
  895. xah->exit();
  896. list_del_init(p);
  897. }
  898. INIT_LIST_HEAD(&xattr_handlers);
  899. }
  900. int __init reiserfs_xattr_register_handlers(void)
  901. {
  902. int err = 0;
  903. struct reiserfs_xattr_handler *xah;
  904. struct list_head *p;
  905. write_lock(&handler_lock);
  906. /* If we're already initialized, nothing to do */
  907. if (!list_empty(&xattr_handlers)) {
  908. write_unlock(&handler_lock);
  909. return 0;
  910. }
  911. /* Add the handlers */
  912. list_add_tail(&user_handler.handlers, &xattr_handlers);
  913. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  914. #ifdef CONFIG_REISERFS_FS_SECURITY
  915. list_add_tail(&security_handler.handlers, &xattr_handlers);
  916. #endif
  917. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  918. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  919. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  920. #endif
  921. /* Run initializers, if available */
  922. list_for_each(p, &xattr_handlers) {
  923. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  924. if (xah->init) {
  925. err = xah->init();
  926. if (err) {
  927. list_del_init(p);
  928. break;
  929. }
  930. }
  931. }
  932. /* Clean up other handlers, if any failed */
  933. if (err)
  934. __unregister_handlers();
  935. write_unlock(&handler_lock);
  936. return err;
  937. }
  938. void reiserfs_xattr_unregister_handlers(void)
  939. {
  940. write_lock(&handler_lock);
  941. __unregister_handlers();
  942. write_unlock(&handler_lock);
  943. }
  944. static int reiserfs_check_acl(struct inode *inode, int mask)
  945. {
  946. struct posix_acl *acl;
  947. int error = -EAGAIN; /* do regular unix permission checks by default */
  948. reiserfs_read_lock_xattr_i(inode);
  949. reiserfs_read_lock_xattrs(inode->i_sb);
  950. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  951. reiserfs_read_unlock_xattrs(inode->i_sb);
  952. reiserfs_read_unlock_xattr_i(inode);
  953. if (acl) {
  954. if (!IS_ERR(acl)) {
  955. error = posix_acl_permission(inode, acl, mask);
  956. posix_acl_release(acl);
  957. } else if (PTR_ERR(acl) != -ENODATA)
  958. error = PTR_ERR(acl);
  959. }
  960. return error;
  961. }
  962. int reiserfs_permission(struct inode *inode, int mask)
  963. {
  964. /*
  965. * We don't do permission checks on the internal objects.
  966. * Permissions are determined by the "owning" object.
  967. */
  968. if (IS_PRIVATE(inode))
  969. return 0;
  970. /*
  971. * Stat data v1 doesn't support ACLs.
  972. */
  973. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  974. return generic_permission(inode, mask, NULL);
  975. else
  976. return generic_permission(inode, mask, reiserfs_check_acl);
  977. }
  978. static int create_privroot(struct dentry *dentry)
  979. {
  980. int err;
  981. struct inode *inode = dentry->d_parent->d_inode;
  982. mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
  983. err = inode->i_op->mkdir(inode, dentry, 0700);
  984. mutex_unlock(&inode->i_mutex);
  985. if (err) {
  986. dput(dentry);
  987. dentry = NULL;
  988. }
  989. if (dentry && dentry->d_inode)
  990. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  991. "storage.\n", PRIVROOT_NAME);
  992. return err;
  993. }
  994. static int xattr_mount_check(struct super_block *s)
  995. {
  996. /* We need generation numbers to ensure that the oid mapping is correct
  997. * v3.5 filesystems don't have them. */
  998. if (!old_format_only(s)) {
  999. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1000. } else if (reiserfs_xattrs_optional(s)) {
  1001. /* Old format filesystem, but optional xattrs have been enabled
  1002. * at mount time. Error out. */
  1003. reiserfs_warning(s, "jdm-20005",
  1004. "xattrs/ACLs not supported on pre v3.6 "
  1005. "format filesystem. Failing mount.");
  1006. return -EOPNOTSUPP;
  1007. } else {
  1008. /* Old format filesystem, but no optional xattrs have
  1009. * been enabled. This means we silently disable xattrs
  1010. * on the filesystem. */
  1011. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1012. }
  1013. return 0;
  1014. }
  1015. #else
  1016. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  1017. void reiserfs_xattr_unregister_handlers(void) {}
  1018. #endif
  1019. /* This will catch lookups from the fs root to .reiserfs_priv */
  1020. static int
  1021. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  1022. {
  1023. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  1024. if (name->len == priv_root->d_name.len &&
  1025. name->hash == priv_root->d_name.hash &&
  1026. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  1027. return -ENOENT;
  1028. } else if (q1->len == name->len &&
  1029. !memcmp(q1->name, name->name, name->len))
  1030. return 0;
  1031. return 1;
  1032. }
  1033. static struct dentry_operations xattr_lookup_poison_ops = {
  1034. .d_compare = xattr_lookup_poison,
  1035. };
  1036. /* We need to take a copy of the mount flags since things like
  1037. * MS_RDONLY don't get set until *after* we're called.
  1038. * mount_flags != mount_options */
  1039. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  1040. {
  1041. int err = 0;
  1042. #ifdef CONFIG_REISERFS_FS_XATTR
  1043. err = xattr_mount_check(s);
  1044. if (err)
  1045. goto error;
  1046. #endif
  1047. /* If we don't have the privroot located yet - go find it */
  1048. if (!REISERFS_SB(s)->priv_root) {
  1049. struct dentry *dentry;
  1050. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1051. strlen(PRIVROOT_NAME));
  1052. if (!IS_ERR(dentry)) {
  1053. #ifdef CONFIG_REISERFS_FS_XATTR
  1054. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode)
  1055. err = create_privroot(dentry);
  1056. #endif
  1057. if (!dentry->d_inode) {
  1058. dput(dentry);
  1059. dentry = NULL;
  1060. }
  1061. } else
  1062. err = PTR_ERR(dentry);
  1063. if (!err && dentry) {
  1064. s->s_root->d_op = &xattr_lookup_poison_ops;
  1065. dentry->d_inode->i_flags |= S_PRIVATE;
  1066. REISERFS_SB(s)->priv_root = dentry;
  1067. #ifdef CONFIG_REISERFS_FS_XATTR
  1068. /* xattrs are unavailable */
  1069. } else if (!(mount_flags & MS_RDONLY)) {
  1070. /* If we're read-only it just means that the dir
  1071. * hasn't been created. Not an error -- just no
  1072. * xattrs on the fs. We'll check again if we
  1073. * go read-write */
  1074. reiserfs_warning(s, "jdm-20006",
  1075. "xattrs/ACLs enabled and couldn't "
  1076. "find/create .reiserfs_priv. "
  1077. "Failing mount.");
  1078. err = -EOPNOTSUPP;
  1079. #endif
  1080. }
  1081. }
  1082. #ifdef CONFIG_REISERFS_FS_XATTR
  1083. error:
  1084. if (err) {
  1085. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1086. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1087. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1088. }
  1089. #endif
  1090. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1091. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1092. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  1093. if (reiserfs_posixacl(s))
  1094. s->s_flags |= MS_POSIXACL;
  1095. #endif
  1096. return err;
  1097. }