xattr.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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 =
  322. reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb);
  323. if (!local_buf) {
  324. pathrelse(&path_to_entry);
  325. return -ENOMEM;
  326. }
  327. if (item_moved(&tmp_ih, &path_to_entry)) {
  328. reiserfs_kfree(local_buf, d_reclen,
  329. inode->i_sb);
  330. /* sigh, must retry. Do this same offset again */
  331. next_pos = d_off;
  332. goto research;
  333. }
  334. }
  335. // Note, that we copy name to user space via temporary
  336. // buffer (local_buf) because filldir will block if
  337. // user space buffer is swapped out. At that time
  338. // entry can move to somewhere else
  339. memcpy(local_buf, d_name, d_reclen);
  340. /* the filldir function might need to start transactions,
  341. * or do who knows what. Release the path now that we've
  342. * copied all the important stuff out of the deh
  343. */
  344. pathrelse(&path_to_entry);
  345. if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
  346. DT_UNKNOWN) < 0) {
  347. if (local_buf != small_buf) {
  348. reiserfs_kfree(local_buf, d_reclen,
  349. inode->i_sb);
  350. }
  351. goto end;
  352. }
  353. if (local_buf != small_buf) {
  354. reiserfs_kfree(local_buf, d_reclen, inode->i_sb);
  355. }
  356. } /* while */
  357. end:
  358. pathrelse(&path_to_entry);
  359. return 0;
  360. }
  361. /*
  362. * this could be done with dedicated readdir ops for the xattr files,
  363. * but I want to get something working asap
  364. * this is stolen from vfs_readdir
  365. *
  366. */
  367. static
  368. int xattr_readdir(struct file *file, filldir_t filler, void *buf)
  369. {
  370. struct inode *inode = file->f_dentry->d_inode;
  371. int res = -ENOTDIR;
  372. if (!file->f_op || !file->f_op->readdir)
  373. goto out;
  374. mutex_lock(&inode->i_mutex);
  375. // down(&inode->i_zombie);
  376. res = -ENOENT;
  377. if (!IS_DEADDIR(inode)) {
  378. lock_kernel();
  379. res = __xattr_readdir(file, buf, filler);
  380. unlock_kernel();
  381. }
  382. // up(&inode->i_zombie);
  383. mutex_unlock(&inode->i_mutex);
  384. out:
  385. return res;
  386. }
  387. /* Internal operations on file data */
  388. static inline void reiserfs_put_page(struct page *page)
  389. {
  390. kunmap(page);
  391. page_cache_release(page);
  392. }
  393. static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
  394. {
  395. struct address_space *mapping = dir->i_mapping;
  396. struct page *page;
  397. /* We can deadlock if we try to free dentries,
  398. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  399. mapping_set_gfp_mask(mapping, GFP_NOFS);
  400. page = read_cache_page(mapping, n,
  401. (filler_t *) mapping->a_ops->readpage, NULL);
  402. if (!IS_ERR(page)) {
  403. wait_on_page_locked(page);
  404. kmap(page);
  405. if (!PageUptodate(page))
  406. goto fail;
  407. if (PageError(page))
  408. goto fail;
  409. }
  410. return page;
  411. fail:
  412. reiserfs_put_page(page);
  413. return ERR_PTR(-EIO);
  414. }
  415. static inline __u32 xattr_hash(const char *msg, int len)
  416. {
  417. return csum_partial(msg, len, 0);
  418. }
  419. /* Generic extended attribute operations that can be used by xa plugins */
  420. /*
  421. * inode->i_mutex: down
  422. */
  423. int
  424. reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
  425. size_t buffer_size, int flags)
  426. {
  427. int err = 0;
  428. struct file *fp;
  429. struct page *page;
  430. char *data;
  431. struct address_space *mapping;
  432. size_t file_pos = 0;
  433. size_t buffer_pos = 0;
  434. struct inode *xinode;
  435. struct iattr newattrs;
  436. __u32 xahash = 0;
  437. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  438. return -EOPNOTSUPP;
  439. /* Empty xattrs are ok, they're just empty files, no hash */
  440. if (buffer && buffer_size)
  441. xahash = xattr_hash(buffer, buffer_size);
  442. open_file:
  443. fp = open_xa_file(inode, name, flags);
  444. if (IS_ERR(fp)) {
  445. err = PTR_ERR(fp);
  446. goto out;
  447. }
  448. xinode = fp->f_dentry->d_inode;
  449. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  450. /* we need to copy it off.. */
  451. if (xinode->i_nlink > 1) {
  452. fput(fp);
  453. err = reiserfs_xattr_del(inode, name);
  454. if (err < 0)
  455. goto out;
  456. /* We just killed the old one, we're not replacing anymore */
  457. if (flags & XATTR_REPLACE)
  458. flags &= ~XATTR_REPLACE;
  459. goto open_file;
  460. }
  461. /* Resize it so we're ok to write there */
  462. newattrs.ia_size = buffer_size;
  463. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  464. mutex_lock(&xinode->i_mutex);
  465. err = notify_change(fp->f_dentry, &newattrs);
  466. if (err)
  467. goto out_filp;
  468. mapping = xinode->i_mapping;
  469. while (buffer_pos < buffer_size || buffer_pos == 0) {
  470. size_t chunk;
  471. size_t skip = 0;
  472. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  473. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  474. chunk = PAGE_CACHE_SIZE;
  475. else
  476. chunk = buffer_size - buffer_pos;
  477. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  478. if (IS_ERR(page)) {
  479. err = PTR_ERR(page);
  480. goto out_filp;
  481. }
  482. lock_page(page);
  483. data = page_address(page);
  484. if (file_pos == 0) {
  485. struct reiserfs_xattr_header *rxh;
  486. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  487. if (chunk + skip > PAGE_CACHE_SIZE)
  488. chunk = PAGE_CACHE_SIZE - skip;
  489. rxh = (struct reiserfs_xattr_header *)data;
  490. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  491. rxh->h_hash = cpu_to_le32(xahash);
  492. }
  493. err = mapping->a_ops->prepare_write(fp, page, page_offset,
  494. page_offset + chunk + skip);
  495. if (!err) {
  496. if (buffer)
  497. memcpy(data + skip, buffer + buffer_pos, chunk);
  498. err =
  499. mapping->a_ops->commit_write(fp, page, page_offset,
  500. page_offset + chunk +
  501. skip);
  502. }
  503. unlock_page(page);
  504. reiserfs_put_page(page);
  505. buffer_pos += chunk;
  506. file_pos += chunk;
  507. skip = 0;
  508. if (err || buffer_size == 0 || !buffer)
  509. break;
  510. }
  511. /* We can't mark the inode dirty if it's not hashed. This is the case
  512. * when we're inheriting the default ACL. If we dirty it, the inode
  513. * gets marked dirty, but won't (ever) make it onto the dirty list until
  514. * it's synced explicitly to clear I_DIRTY. This is bad. */
  515. if (!hlist_unhashed(&inode->i_hash)) {
  516. inode->i_ctime = CURRENT_TIME_SEC;
  517. mark_inode_dirty(inode);
  518. }
  519. out_filp:
  520. mutex_unlock(&xinode->i_mutex);
  521. fput(fp);
  522. out:
  523. return err;
  524. }
  525. /*
  526. * inode->i_mutex: down
  527. */
  528. int
  529. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  530. size_t buffer_size)
  531. {
  532. ssize_t err = 0;
  533. struct file *fp;
  534. size_t isize;
  535. size_t file_pos = 0;
  536. size_t buffer_pos = 0;
  537. struct page *page;
  538. struct inode *xinode;
  539. __u32 hash = 0;
  540. if (name == NULL)
  541. return -EINVAL;
  542. /* We can't have xattrs attached to v1 items since they don't have
  543. * generation numbers */
  544. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  545. return -EOPNOTSUPP;
  546. fp = open_xa_file(inode, name, FL_READONLY);
  547. if (IS_ERR(fp)) {
  548. err = PTR_ERR(fp);
  549. goto out;
  550. }
  551. xinode = fp->f_dentry->d_inode;
  552. isize = xinode->i_size;
  553. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  554. /* Just return the size needed */
  555. if (buffer == NULL) {
  556. err = isize - sizeof(struct reiserfs_xattr_header);
  557. goto out_dput;
  558. }
  559. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  560. err = -ERANGE;
  561. goto out_dput;
  562. }
  563. while (file_pos < isize) {
  564. size_t chunk;
  565. char *data;
  566. size_t skip = 0;
  567. if (isize - file_pos > PAGE_CACHE_SIZE)
  568. chunk = PAGE_CACHE_SIZE;
  569. else
  570. chunk = isize - file_pos;
  571. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  572. if (IS_ERR(page)) {
  573. err = PTR_ERR(page);
  574. goto out_dput;
  575. }
  576. lock_page(page);
  577. data = page_address(page);
  578. if (file_pos == 0) {
  579. struct reiserfs_xattr_header *rxh =
  580. (struct reiserfs_xattr_header *)data;
  581. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  582. chunk -= skip;
  583. /* Magic doesn't match up.. */
  584. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  585. unlock_page(page);
  586. reiserfs_put_page(page);
  587. reiserfs_warning(inode->i_sb,
  588. "Invalid magic for xattr (%s) "
  589. "associated with %k", name,
  590. INODE_PKEY(inode));
  591. err = -EIO;
  592. goto out_dput;
  593. }
  594. hash = le32_to_cpu(rxh->h_hash);
  595. }
  596. memcpy(buffer + buffer_pos, data + skip, chunk);
  597. unlock_page(page);
  598. reiserfs_put_page(page);
  599. file_pos += chunk;
  600. buffer_pos += chunk;
  601. skip = 0;
  602. }
  603. err = isize - sizeof(struct reiserfs_xattr_header);
  604. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  605. hash) {
  606. reiserfs_warning(inode->i_sb,
  607. "Invalid hash for xattr (%s) associated "
  608. "with %k", name, INODE_PKEY(inode));
  609. err = -EIO;
  610. }
  611. out_dput:
  612. fput(fp);
  613. out:
  614. return err;
  615. }
  616. static int
  617. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  618. {
  619. struct dentry *dentry;
  620. struct inode *dir = xadir->d_inode;
  621. int err = 0;
  622. dentry = lookup_one_len(name, xadir, namelen);
  623. if (IS_ERR(dentry)) {
  624. err = PTR_ERR(dentry);
  625. goto out;
  626. } else if (!dentry->d_inode) {
  627. err = -ENODATA;
  628. goto out_file;
  629. }
  630. /* Skip directories.. */
  631. if (S_ISDIR(dentry->d_inode->i_mode))
  632. goto out_file;
  633. if (!is_reiserfs_priv_object(dentry->d_inode)) {
  634. reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
  635. "priv flag set [parent is %sset].",
  636. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  637. k_objectid), xadir->d_name.len,
  638. xadir->d_name.name, namelen, name,
  639. is_reiserfs_priv_object(xadir->
  640. d_inode) ? "" :
  641. "not ");
  642. dput(dentry);
  643. return -EIO;
  644. }
  645. err = dir->i_op->unlink(dir, dentry);
  646. if (!err)
  647. d_delete(dentry);
  648. out_file:
  649. dput(dentry);
  650. out:
  651. return err;
  652. }
  653. int reiserfs_xattr_del(struct inode *inode, const char *name)
  654. {
  655. struct dentry *dir;
  656. int err;
  657. dir = open_xa_dir(inode, FL_READONLY);
  658. if (IS_ERR(dir)) {
  659. err = PTR_ERR(dir);
  660. goto out;
  661. }
  662. err = __reiserfs_xattr_del(dir, name, strlen(name));
  663. dput(dir);
  664. if (!err) {
  665. inode->i_ctime = CURRENT_TIME_SEC;
  666. mark_inode_dirty(inode);
  667. }
  668. out:
  669. return err;
  670. }
  671. /* The following are side effects of other operations that aren't explicitly
  672. * modifying extended attributes. This includes operations such as permissions
  673. * or ownership changes, object deletions, etc. */
  674. static int
  675. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  676. loff_t offset, ino_t ino, unsigned int d_type)
  677. {
  678. struct dentry *xadir = (struct dentry *)buf;
  679. return __reiserfs_xattr_del(xadir, name, namelen);
  680. }
  681. /* This is called w/ inode->i_mutex downed */
  682. int reiserfs_delete_xattrs(struct inode *inode)
  683. {
  684. struct file *fp;
  685. struct dentry *dir, *root;
  686. int err = 0;
  687. /* Skip out, an xattr has no xattrs associated with it */
  688. if (is_reiserfs_priv_object(inode) ||
  689. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  690. !reiserfs_xattrs(inode->i_sb)) {
  691. return 0;
  692. }
  693. reiserfs_read_lock_xattrs(inode->i_sb);
  694. dir = open_xa_dir(inode, FL_READONLY);
  695. reiserfs_read_unlock_xattrs(inode->i_sb);
  696. if (IS_ERR(dir)) {
  697. err = PTR_ERR(dir);
  698. goto out;
  699. } else if (!dir->d_inode) {
  700. dput(dir);
  701. return 0;
  702. }
  703. fp = dentry_open(dir, NULL, O_RDWR);
  704. if (IS_ERR(fp)) {
  705. err = PTR_ERR(fp);
  706. /* dentry_open dputs the dentry if it fails */
  707. goto out;
  708. }
  709. lock_kernel();
  710. err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
  711. if (err) {
  712. unlock_kernel();
  713. goto out_dir;
  714. }
  715. /* Leftovers besides . and .. -- that's not good. */
  716. if (dir->d_inode->i_nlink <= 2) {
  717. root = get_xa_root(inode->i_sb);
  718. reiserfs_write_lock_xattrs(inode->i_sb);
  719. err = vfs_rmdir(root->d_inode, dir);
  720. reiserfs_write_unlock_xattrs(inode->i_sb);
  721. dput(root);
  722. } else {
  723. reiserfs_warning(inode->i_sb,
  724. "Couldn't remove all entries in directory");
  725. }
  726. unlock_kernel();
  727. out_dir:
  728. fput(fp);
  729. out:
  730. if (!err)
  731. REISERFS_I(inode)->i_flags =
  732. REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  733. return err;
  734. }
  735. struct reiserfs_chown_buf {
  736. struct inode *inode;
  737. struct dentry *xadir;
  738. struct iattr *attrs;
  739. };
  740. /* XXX: If there is a better way to do this, I'd love to hear about it */
  741. static int
  742. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  743. loff_t offset, ino_t ino, unsigned int d_type)
  744. {
  745. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  746. struct dentry *xafile, *xadir = chown_buf->xadir;
  747. struct iattr *attrs = chown_buf->attrs;
  748. int err = 0;
  749. xafile = lookup_one_len(name, xadir, namelen);
  750. if (IS_ERR(xafile))
  751. return PTR_ERR(xafile);
  752. else if (!xafile->d_inode) {
  753. dput(xafile);
  754. return -ENODATA;
  755. }
  756. if (!S_ISDIR(xafile->d_inode->i_mode))
  757. err = notify_change(xafile, attrs);
  758. dput(xafile);
  759. return err;
  760. }
  761. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  762. {
  763. struct file *fp;
  764. struct dentry *dir;
  765. int err = 0;
  766. struct reiserfs_chown_buf buf;
  767. unsigned int ia_valid = attrs->ia_valid;
  768. /* Skip out, an xattr has no xattrs associated with it */
  769. if (is_reiserfs_priv_object(inode) ||
  770. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  771. !reiserfs_xattrs(inode->i_sb)) {
  772. return 0;
  773. }
  774. reiserfs_read_lock_xattrs(inode->i_sb);
  775. dir = open_xa_dir(inode, FL_READONLY);
  776. reiserfs_read_unlock_xattrs(inode->i_sb);
  777. if (IS_ERR(dir)) {
  778. if (PTR_ERR(dir) != -ENODATA)
  779. err = PTR_ERR(dir);
  780. goto out;
  781. } else if (!dir->d_inode) {
  782. dput(dir);
  783. goto out;
  784. }
  785. fp = dentry_open(dir, NULL, O_RDWR);
  786. if (IS_ERR(fp)) {
  787. err = PTR_ERR(fp);
  788. /* dentry_open dputs the dentry if it fails */
  789. goto out;
  790. }
  791. lock_kernel();
  792. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  793. buf.xadir = dir;
  794. buf.attrs = attrs;
  795. buf.inode = inode;
  796. err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
  797. if (err) {
  798. unlock_kernel();
  799. goto out_dir;
  800. }
  801. err = notify_change(dir, attrs);
  802. unlock_kernel();
  803. out_dir:
  804. fput(fp);
  805. out:
  806. attrs->ia_valid = ia_valid;
  807. return err;
  808. }
  809. /* Actual operations that are exported to VFS-land */
  810. /*
  811. * Inode operation getxattr()
  812. * Preliminary locking: we down dentry->d_inode->i_mutex
  813. */
  814. ssize_t
  815. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  816. size_t size)
  817. {
  818. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  819. int err;
  820. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  821. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  822. return -EOPNOTSUPP;
  823. reiserfs_read_lock_xattr_i(dentry->d_inode);
  824. reiserfs_read_lock_xattrs(dentry->d_sb);
  825. err = xah->get(dentry->d_inode, name, buffer, size);
  826. reiserfs_read_unlock_xattrs(dentry->d_sb);
  827. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  828. return err;
  829. }
  830. /*
  831. * Inode operation setxattr()
  832. *
  833. * dentry->d_inode->i_mutex down
  834. */
  835. int
  836. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  837. size_t size, int flags)
  838. {
  839. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  840. int err;
  841. int lock;
  842. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  843. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  844. return -EOPNOTSUPP;
  845. reiserfs_write_lock_xattr_i(dentry->d_inode);
  846. lock = !has_xattr_dir(dentry->d_inode);
  847. if (lock)
  848. reiserfs_write_lock_xattrs(dentry->d_sb);
  849. else
  850. reiserfs_read_lock_xattrs(dentry->d_sb);
  851. err = xah->set(dentry->d_inode, name, value, size, flags);
  852. if (lock)
  853. reiserfs_write_unlock_xattrs(dentry->d_sb);
  854. else
  855. reiserfs_read_unlock_xattrs(dentry->d_sb);
  856. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  857. return err;
  858. }
  859. /*
  860. * Inode operation removexattr()
  861. *
  862. * dentry->d_inode->i_mutex down
  863. */
  864. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  865. {
  866. int err;
  867. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  868. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  869. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  870. return -EOPNOTSUPP;
  871. reiserfs_write_lock_xattr_i(dentry->d_inode);
  872. reiserfs_read_lock_xattrs(dentry->d_sb);
  873. /* Deletion pre-operation */
  874. if (xah->del) {
  875. err = xah->del(dentry->d_inode, name);
  876. if (err)
  877. goto out;
  878. }
  879. err = reiserfs_xattr_del(dentry->d_inode, name);
  880. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  881. mark_inode_dirty(dentry->d_inode);
  882. out:
  883. reiserfs_read_unlock_xattrs(dentry->d_sb);
  884. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  885. return err;
  886. }
  887. /* This is what filldir will use:
  888. * r_pos will always contain the amount of space required for the entire
  889. * list. If r_pos becomes larger than r_size, we need more space and we
  890. * return an error indicating this. If r_pos is less than r_size, then we've
  891. * filled the buffer successfully and we return success */
  892. struct reiserfs_listxattr_buf {
  893. int r_pos;
  894. int r_size;
  895. char *r_buf;
  896. struct inode *r_inode;
  897. };
  898. static int
  899. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  900. loff_t offset, ino_t ino, unsigned int d_type)
  901. {
  902. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  903. int len = 0;
  904. if (name[0] != '.'
  905. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  906. struct reiserfs_xattr_handler *xah =
  907. find_xattr_handler_prefix(name);
  908. if (!xah)
  909. return 0; /* Unsupported xattr name, skip it */
  910. /* We call ->list() twice because the operation isn't required to just
  911. * return the name back - we want to make sure we have enough space */
  912. len += xah->list(b->r_inode, name, namelen, NULL);
  913. if (len) {
  914. if (b->r_pos + len + 1 <= b->r_size) {
  915. char *p = b->r_buf + b->r_pos;
  916. p += xah->list(b->r_inode, name, namelen, p);
  917. *p++ = '\0';
  918. }
  919. b->r_pos += len + 1;
  920. }
  921. }
  922. return 0;
  923. }
  924. /*
  925. * Inode operation listxattr()
  926. *
  927. * Preliminary locking: we down dentry->d_inode->i_mutex
  928. */
  929. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  930. {
  931. struct file *fp;
  932. struct dentry *dir;
  933. int err = 0;
  934. struct reiserfs_listxattr_buf buf;
  935. if (!dentry->d_inode)
  936. return -EINVAL;
  937. if (!reiserfs_xattrs(dentry->d_sb) ||
  938. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  939. return -EOPNOTSUPP;
  940. reiserfs_read_lock_xattr_i(dentry->d_inode);
  941. reiserfs_read_lock_xattrs(dentry->d_sb);
  942. dir = open_xa_dir(dentry->d_inode, FL_READONLY);
  943. reiserfs_read_unlock_xattrs(dentry->d_sb);
  944. if (IS_ERR(dir)) {
  945. err = PTR_ERR(dir);
  946. if (err == -ENODATA)
  947. err = 0; /* Not an error if there aren't any xattrs */
  948. goto out;
  949. }
  950. fp = dentry_open(dir, NULL, O_RDWR);
  951. if (IS_ERR(fp)) {
  952. err = PTR_ERR(fp);
  953. /* dentry_open dputs the dentry if it fails */
  954. goto out;
  955. }
  956. buf.r_buf = buffer;
  957. buf.r_size = buffer ? size : 0;
  958. buf.r_pos = 0;
  959. buf.r_inode = dentry->d_inode;
  960. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  961. err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
  962. if (err)
  963. goto out_dir;
  964. if (buf.r_pos > buf.r_size && buffer != NULL)
  965. err = -ERANGE;
  966. else
  967. err = buf.r_pos;
  968. out_dir:
  969. fput(fp);
  970. out:
  971. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  972. return err;
  973. }
  974. /* This is the implementation for the xattr plugin infrastructure */
  975. static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
  976. static DEFINE_RWLOCK(handler_lock);
  977. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  978. *prefix)
  979. {
  980. struct reiserfs_xattr_handler *xah = NULL;
  981. struct list_head *p;
  982. read_lock(&handler_lock);
  983. list_for_each(p, &xattr_handlers) {
  984. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  985. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  986. break;
  987. xah = NULL;
  988. }
  989. read_unlock(&handler_lock);
  990. return xah;
  991. }
  992. static void __unregister_handlers(void)
  993. {
  994. struct reiserfs_xattr_handler *xah;
  995. struct list_head *p, *tmp;
  996. list_for_each_safe(p, tmp, &xattr_handlers) {
  997. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  998. if (xah->exit)
  999. xah->exit();
  1000. list_del_init(p);
  1001. }
  1002. INIT_LIST_HEAD(&xattr_handlers);
  1003. }
  1004. int __init reiserfs_xattr_register_handlers(void)
  1005. {
  1006. int err = 0;
  1007. struct reiserfs_xattr_handler *xah;
  1008. struct list_head *p;
  1009. write_lock(&handler_lock);
  1010. /* If we're already initialized, nothing to do */
  1011. if (!list_empty(&xattr_handlers)) {
  1012. write_unlock(&handler_lock);
  1013. return 0;
  1014. }
  1015. /* Add the handlers */
  1016. list_add_tail(&user_handler.handlers, &xattr_handlers);
  1017. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  1018. #ifdef CONFIG_REISERFS_FS_SECURITY
  1019. list_add_tail(&security_handler.handlers, &xattr_handlers);
  1020. #endif
  1021. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  1022. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  1023. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  1024. #endif
  1025. /* Run initializers, if available */
  1026. list_for_each(p, &xattr_handlers) {
  1027. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  1028. if (xah->init) {
  1029. err = xah->init();
  1030. if (err) {
  1031. list_del_init(p);
  1032. break;
  1033. }
  1034. }
  1035. }
  1036. /* Clean up other handlers, if any failed */
  1037. if (err)
  1038. __unregister_handlers();
  1039. write_unlock(&handler_lock);
  1040. return err;
  1041. }
  1042. void reiserfs_xattr_unregister_handlers(void)
  1043. {
  1044. write_lock(&handler_lock);
  1045. __unregister_handlers();
  1046. write_unlock(&handler_lock);
  1047. }
  1048. /* This will catch lookups from the fs root to .reiserfs_priv */
  1049. static int
  1050. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  1051. {
  1052. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  1053. if (name->len == priv_root->d_name.len &&
  1054. name->hash == priv_root->d_name.hash &&
  1055. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  1056. return -ENOENT;
  1057. } else if (q1->len == name->len &&
  1058. !memcmp(q1->name, name->name, name->len))
  1059. return 0;
  1060. return 1;
  1061. }
  1062. static struct dentry_operations xattr_lookup_poison_ops = {
  1063. .d_compare = xattr_lookup_poison,
  1064. };
  1065. /* We need to take a copy of the mount flags since things like
  1066. * MS_RDONLY don't get set until *after* we're called.
  1067. * mount_flags != mount_options */
  1068. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  1069. {
  1070. int err = 0;
  1071. /* We need generation numbers to ensure that the oid mapping is correct
  1072. * v3.5 filesystems don't have them. */
  1073. if (!old_format_only(s)) {
  1074. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1075. } else if (reiserfs_xattrs_optional(s)) {
  1076. /* Old format filesystem, but optional xattrs have been enabled
  1077. * at mount time. Error out. */
  1078. reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
  1079. "format filesystem. Failing mount.");
  1080. err = -EOPNOTSUPP;
  1081. goto error;
  1082. } else {
  1083. /* Old format filesystem, but no optional xattrs have been enabled. This
  1084. * means we silently disable xattrs on the filesystem. */
  1085. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1086. }
  1087. /* If we don't have the privroot located yet - go find it */
  1088. if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
  1089. struct dentry *dentry;
  1090. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1091. strlen(PRIVROOT_NAME));
  1092. if (!IS_ERR(dentry)) {
  1093. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
  1094. struct inode *inode = dentry->d_parent->d_inode;
  1095. mutex_lock(&inode->i_mutex);
  1096. err = inode->i_op->mkdir(inode, dentry, 0700);
  1097. mutex_unlock(&inode->i_mutex);
  1098. if (err) {
  1099. dput(dentry);
  1100. dentry = NULL;
  1101. }
  1102. if (dentry && dentry->d_inode)
  1103. reiserfs_warning(s,
  1104. "Created %s on %s - reserved for "
  1105. "xattr storage.",
  1106. PRIVROOT_NAME,
  1107. reiserfs_bdevname
  1108. (inode->i_sb));
  1109. } else if (!dentry->d_inode) {
  1110. dput(dentry);
  1111. dentry = NULL;
  1112. }
  1113. } else
  1114. err = PTR_ERR(dentry);
  1115. if (!err && dentry) {
  1116. s->s_root->d_op = &xattr_lookup_poison_ops;
  1117. reiserfs_mark_inode_private(dentry->d_inode);
  1118. REISERFS_SB(s)->priv_root = dentry;
  1119. } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
  1120. /* If we're read-only it just means that the dir hasn't been
  1121. * created. Not an error -- just no xattrs on the fs. We'll
  1122. * check again if we go read-write */
  1123. reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
  1124. "find/create .reiserfs_priv. Failing mount.");
  1125. err = -EOPNOTSUPP;
  1126. }
  1127. }
  1128. error:
  1129. /* This is only nonzero if there was an error initializing the xattr
  1130. * directory or if there is a condition where we don't support them. */
  1131. if (err) {
  1132. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1133. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1134. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1135. }
  1136. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1137. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1138. if (reiserfs_posixacl(s))
  1139. s->s_flags |= MS_POSIXACL;
  1140. return err;
  1141. }
  1142. static int
  1143. __reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
  1144. int need_lock)
  1145. {
  1146. umode_t mode = inode->i_mode;
  1147. if (mask & MAY_WRITE) {
  1148. /*
  1149. * Nobody gets write access to a read-only fs.
  1150. */
  1151. if (IS_RDONLY(inode) &&
  1152. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  1153. return -EROFS;
  1154. /*
  1155. * Nobody gets write access to an immutable file.
  1156. */
  1157. if (IS_IMMUTABLE(inode))
  1158. return -EACCES;
  1159. }
  1160. /* We don't do permission checks on the internal objects.
  1161. * Permissions are determined by the "owning" object. */
  1162. if (is_reiserfs_priv_object(inode))
  1163. return 0;
  1164. if (current->fsuid == inode->i_uid) {
  1165. mode >>= 6;
  1166. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  1167. } else if (reiserfs_posixacl(inode->i_sb) &&
  1168. get_inode_sd_version(inode) != STAT_DATA_V1) {
  1169. struct posix_acl *acl;
  1170. /* ACL can't contain additional permissions if
  1171. the ACL_MASK entry is 0 */
  1172. if (!(mode & S_IRWXG))
  1173. goto check_groups;
  1174. if (need_lock) {
  1175. reiserfs_read_lock_xattr_i(inode);
  1176. reiserfs_read_lock_xattrs(inode->i_sb);
  1177. }
  1178. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  1179. if (need_lock) {
  1180. reiserfs_read_unlock_xattrs(inode->i_sb);
  1181. reiserfs_read_unlock_xattr_i(inode);
  1182. }
  1183. if (IS_ERR(acl)) {
  1184. if (PTR_ERR(acl) == -ENODATA)
  1185. goto check_groups;
  1186. return PTR_ERR(acl);
  1187. }
  1188. if (acl) {
  1189. int err = posix_acl_permission(inode, acl, mask);
  1190. posix_acl_release(acl);
  1191. if (err == -EACCES) {
  1192. goto check_capabilities;
  1193. }
  1194. return err;
  1195. } else {
  1196. goto check_groups;
  1197. }
  1198. #endif
  1199. } else {
  1200. check_groups:
  1201. if (in_group_p(inode->i_gid))
  1202. mode >>= 3;
  1203. }
  1204. /*
  1205. * If the DACs are ok we don't need any capability check.
  1206. */
  1207. if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
  1208. return 0;
  1209. check_capabilities:
  1210. /*
  1211. * Read/write DACs are always overridable.
  1212. * Executable DACs are overridable if at least one exec bit is set.
  1213. */
  1214. if (!(mask & MAY_EXEC) ||
  1215. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  1216. if (capable(CAP_DAC_OVERRIDE))
  1217. return 0;
  1218. /*
  1219. * Searching includes executable on directories, else just read.
  1220. */
  1221. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  1222. if (capable(CAP_DAC_READ_SEARCH))
  1223. return 0;
  1224. return -EACCES;
  1225. }
  1226. int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  1227. {
  1228. return __reiserfs_permission(inode, mask, nd, 1);
  1229. }
  1230. int
  1231. reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
  1232. {
  1233. return __reiserfs_permission(inode, mask, nd, 0);
  1234. }