xattr.c 34 KB

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