inode.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/sort.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include <linux/crc32.h>
  18. #include <linux/lm_interface.h>
  19. #include <linux/security.h>
  20. #include "gfs2.h"
  21. #include "incore.h"
  22. #include "acl.h"
  23. #include "bmap.h"
  24. #include "dir.h"
  25. #include "eattr.h"
  26. #include "glock.h"
  27. #include "glops.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "ops_address.h"
  32. #include "ops_file.h"
  33. #include "ops_inode.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. static int iget_test(struct inode *inode, void *opaque)
  39. {
  40. struct gfs2_inode *ip = GFS2_I(inode);
  41. struct gfs2_inum_host *inum = opaque;
  42. if (ip->i_num.no_addr == inum->no_addr)
  43. return 1;
  44. return 0;
  45. }
  46. static int iget_set(struct inode *inode, void *opaque)
  47. {
  48. struct gfs2_inode *ip = GFS2_I(inode);
  49. struct gfs2_inum_host *inum = opaque;
  50. ip->i_num = *inum;
  51. inode->i_ino = inum->no_addr;
  52. return 0;
  53. }
  54. struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum)
  55. {
  56. return ilookup5(sb, (unsigned long)inum->no_formal_ino,
  57. iget_test, inum);
  58. }
  59. static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum)
  60. {
  61. return iget5_locked(sb, (unsigned long)inum->no_formal_ino,
  62. iget_test, iget_set, inum);
  63. }
  64. /**
  65. * gfs2_inode_lookup - Lookup an inode
  66. * @sb: The super block
  67. * @inum: The inode number
  68. * @type: The type of the inode
  69. *
  70. * Returns: A VFS inode, or an error
  71. */
  72. struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type)
  73. {
  74. struct inode *inode = gfs2_iget(sb, inum);
  75. struct gfs2_inode *ip = GFS2_I(inode);
  76. struct gfs2_glock *io_gl;
  77. int error;
  78. if (!inode)
  79. return ERR_PTR(-ENOBUFS);
  80. if (inode->i_state & I_NEW) {
  81. struct gfs2_sbd *sdp = GFS2_SB(inode);
  82. umode_t mode = DT2IF(type);
  83. inode->i_private = ip;
  84. inode->i_mode = mode;
  85. if (S_ISREG(mode)) {
  86. inode->i_op = &gfs2_file_iops;
  87. inode->i_fop = &gfs2_file_fops;
  88. inode->i_mapping->a_ops = &gfs2_file_aops;
  89. } else if (S_ISDIR(mode)) {
  90. inode->i_op = &gfs2_dir_iops;
  91. inode->i_fop = &gfs2_dir_fops;
  92. } else if (S_ISLNK(mode)) {
  93. inode->i_op = &gfs2_symlink_iops;
  94. } else {
  95. inode->i_op = &gfs2_dev_iops;
  96. }
  97. error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  98. if (unlikely(error))
  99. goto fail;
  100. ip->i_gl->gl_object = ip;
  101. error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  102. if (unlikely(error))
  103. goto fail_put;
  104. set_bit(GIF_INVALID, &ip->i_flags);
  105. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  106. if (unlikely(error))
  107. goto fail_iopen;
  108. gfs2_glock_put(io_gl);
  109. unlock_new_inode(inode);
  110. }
  111. return inode;
  112. fail_iopen:
  113. gfs2_glock_put(io_gl);
  114. fail_put:
  115. ip->i_gl->gl_object = NULL;
  116. gfs2_glock_put(ip->i_gl);
  117. fail:
  118. iput(inode);
  119. return ERR_PTR(error);
  120. }
  121. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  122. {
  123. struct gfs2_dinode_host *di = &ip->i_di;
  124. const struct gfs2_dinode *str = buf;
  125. if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) {
  126. if (gfs2_consist_inode(ip))
  127. gfs2_dinode_print(ip);
  128. return -EIO;
  129. }
  130. if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino))
  131. return -ESTALE;
  132. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  133. ip->i_inode.i_rdev = 0;
  134. switch (ip->i_inode.i_mode & S_IFMT) {
  135. case S_IFBLK:
  136. case S_IFCHR:
  137. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  138. be32_to_cpu(str->di_minor));
  139. break;
  140. };
  141. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  142. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  143. /*
  144. * We will need to review setting the nlink count here in the
  145. * light of the forthcoming ro bind mount work. This is a reminder
  146. * to do that.
  147. */
  148. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  149. di->di_size = be64_to_cpu(str->di_size);
  150. i_size_write(&ip->i_inode, di->di_size);
  151. di->di_blocks = be64_to_cpu(str->di_blocks);
  152. gfs2_set_inode_blocks(&ip->i_inode);
  153. ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
  154. ip->i_inode.i_atime.tv_nsec = 0;
  155. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  156. ip->i_inode.i_mtime.tv_nsec = 0;
  157. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  158. ip->i_inode.i_ctime.tv_nsec = 0;
  159. di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
  160. di->di_goal_data = be64_to_cpu(str->di_goal_data);
  161. di->di_generation = be64_to_cpu(str->di_generation);
  162. di->di_flags = be32_to_cpu(str->di_flags);
  163. gfs2_set_inode_flags(&ip->i_inode);
  164. di->di_height = be16_to_cpu(str->di_height);
  165. di->di_depth = be16_to_cpu(str->di_depth);
  166. di->di_entries = be32_to_cpu(str->di_entries);
  167. di->di_eattr = be64_to_cpu(str->di_eattr);
  168. return 0;
  169. }
  170. /**
  171. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  172. * @ip: The GFS2 inode
  173. *
  174. * Returns: errno
  175. */
  176. int gfs2_inode_refresh(struct gfs2_inode *ip)
  177. {
  178. struct buffer_head *dibh;
  179. int error;
  180. error = gfs2_meta_inode_buffer(ip, &dibh);
  181. if (error)
  182. return error;
  183. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  184. brelse(dibh);
  185. return -EIO;
  186. }
  187. error = gfs2_dinode_in(ip, dibh->b_data);
  188. brelse(dibh);
  189. clear_bit(GIF_INVALID, &ip->i_flags);
  190. return error;
  191. }
  192. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  193. {
  194. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  195. struct gfs2_alloc *al;
  196. struct gfs2_rgrpd *rgd;
  197. int error;
  198. if (ip->i_di.di_blocks != 1) {
  199. if (gfs2_consist_inode(ip))
  200. gfs2_dinode_print(ip);
  201. return -EIO;
  202. }
  203. al = gfs2_alloc_get(ip);
  204. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  205. if (error)
  206. goto out;
  207. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  208. if (error)
  209. goto out_qs;
  210. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  211. if (!rgd) {
  212. gfs2_consist_inode(ip);
  213. error = -EIO;
  214. goto out_rindex_relse;
  215. }
  216. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  217. &al->al_rgd_gh);
  218. if (error)
  219. goto out_rindex_relse;
  220. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  221. if (error)
  222. goto out_rg_gunlock;
  223. gfs2_trans_add_gl(ip->i_gl);
  224. gfs2_free_di(rgd, ip);
  225. gfs2_trans_end(sdp);
  226. clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  227. out_rg_gunlock:
  228. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  229. out_rindex_relse:
  230. gfs2_glock_dq_uninit(&al->al_ri_gh);
  231. out_qs:
  232. gfs2_quota_unhold(ip);
  233. out:
  234. gfs2_alloc_put(ip);
  235. return error;
  236. }
  237. /**
  238. * gfs2_change_nlink - Change nlink count on inode
  239. * @ip: The GFS2 inode
  240. * @diff: The change in the nlink count required
  241. *
  242. * Returns: errno
  243. */
  244. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  245. {
  246. struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info;
  247. struct buffer_head *dibh;
  248. u32 nlink;
  249. int error;
  250. BUG_ON(diff != 1 && diff != -1);
  251. nlink = ip->i_inode.i_nlink + diff;
  252. /* If we are reducing the nlink count, but the new value ends up being
  253. bigger than the old one, we must have underflowed. */
  254. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  255. if (gfs2_consist_inode(ip))
  256. gfs2_dinode_print(ip);
  257. return -EIO;
  258. }
  259. error = gfs2_meta_inode_buffer(ip, &dibh);
  260. if (error)
  261. return error;
  262. if (diff > 0)
  263. inc_nlink(&ip->i_inode);
  264. else
  265. drop_nlink(&ip->i_inode);
  266. ip->i_inode.i_ctime.tv_sec = get_seconds();
  267. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  268. gfs2_dinode_out(ip, dibh->b_data);
  269. brelse(dibh);
  270. mark_inode_dirty(&ip->i_inode);
  271. if (ip->i_inode.i_nlink == 0) {
  272. struct gfs2_rgrpd *rgd;
  273. struct gfs2_holder ri_gh, rg_gh;
  274. error = gfs2_rindex_hold(sdp, &ri_gh);
  275. if (error)
  276. goto out;
  277. error = -EIO;
  278. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  279. if (!rgd)
  280. goto out_norgrp;
  281. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
  282. if (error)
  283. goto out_norgrp;
  284. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  285. gfs2_glock_dq_uninit(&rg_gh);
  286. out_norgrp:
  287. gfs2_glock_dq_uninit(&ri_gh);
  288. }
  289. out:
  290. return error;
  291. }
  292. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  293. {
  294. struct qstr qstr;
  295. gfs2_str2qstr(&qstr, name);
  296. return gfs2_lookupi(dip, &qstr, 1, NULL);
  297. }
  298. /**
  299. * gfs2_lookupi - Look up a filename in a directory and return its inode
  300. * @d_gh: An initialized holder for the directory glock
  301. * @name: The name of the inode to look for
  302. * @is_root: If 1, ignore the caller's permissions
  303. * @i_gh: An uninitialized holder for the new inode glock
  304. *
  305. * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
  306. * @is_root is true.
  307. *
  308. * Returns: errno
  309. */
  310. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  311. int is_root, struct nameidata *nd)
  312. {
  313. struct super_block *sb = dir->i_sb;
  314. struct gfs2_inode *dip = GFS2_I(dir);
  315. struct gfs2_holder d_gh;
  316. struct gfs2_inum_host inum;
  317. unsigned int type;
  318. int error = 0;
  319. struct inode *inode = NULL;
  320. if (!name->len || name->len > GFS2_FNAMESIZE)
  321. return ERR_PTR(-ENAMETOOLONG);
  322. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  323. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  324. dir == sb->s_root->d_inode)) {
  325. igrab(dir);
  326. return dir;
  327. }
  328. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  329. if (error)
  330. return ERR_PTR(error);
  331. if (!is_root) {
  332. error = permission(dir, MAY_EXEC, NULL);
  333. if (error)
  334. goto out;
  335. }
  336. error = gfs2_dir_search(dir, name, &inum, &type);
  337. if (error)
  338. goto out;
  339. inode = gfs2_inode_lookup(sb, &inum, type);
  340. out:
  341. gfs2_glock_dq_uninit(&d_gh);
  342. if (error == -ENOENT)
  343. return NULL;
  344. return inode;
  345. }
  346. static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
  347. {
  348. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  349. struct buffer_head *bh;
  350. struct gfs2_inum_range_host ir;
  351. int error;
  352. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  353. if (error)
  354. return error;
  355. mutex_lock(&sdp->sd_inum_mutex);
  356. error = gfs2_meta_inode_buffer(ip, &bh);
  357. if (error) {
  358. mutex_unlock(&sdp->sd_inum_mutex);
  359. gfs2_trans_end(sdp);
  360. return error;
  361. }
  362. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  363. if (ir.ir_length) {
  364. *formal_ino = ir.ir_start++;
  365. ir.ir_length--;
  366. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  367. gfs2_inum_range_out(&ir,
  368. bh->b_data + sizeof(struct gfs2_dinode));
  369. brelse(bh);
  370. mutex_unlock(&sdp->sd_inum_mutex);
  371. gfs2_trans_end(sdp);
  372. return 0;
  373. }
  374. brelse(bh);
  375. mutex_unlock(&sdp->sd_inum_mutex);
  376. gfs2_trans_end(sdp);
  377. return 1;
  378. }
  379. static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
  380. {
  381. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  382. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
  383. struct gfs2_holder gh;
  384. struct buffer_head *bh;
  385. struct gfs2_inum_range_host ir;
  386. int error;
  387. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  388. if (error)
  389. return error;
  390. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  391. if (error)
  392. goto out;
  393. mutex_lock(&sdp->sd_inum_mutex);
  394. error = gfs2_meta_inode_buffer(ip, &bh);
  395. if (error)
  396. goto out_end_trans;
  397. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  398. if (!ir.ir_length) {
  399. struct buffer_head *m_bh;
  400. u64 x, y;
  401. __be64 z;
  402. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  403. if (error)
  404. goto out_brelse;
  405. z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
  406. x = y = be64_to_cpu(z);
  407. ir.ir_start = x;
  408. ir.ir_length = GFS2_INUM_QUANTUM;
  409. x += GFS2_INUM_QUANTUM;
  410. if (x < y)
  411. gfs2_consist_inode(m_ip);
  412. z = cpu_to_be64(x);
  413. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  414. *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
  415. brelse(m_bh);
  416. }
  417. *formal_ino = ir.ir_start++;
  418. ir.ir_length--;
  419. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  420. gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  421. out_brelse:
  422. brelse(bh);
  423. out_end_trans:
  424. mutex_unlock(&sdp->sd_inum_mutex);
  425. gfs2_trans_end(sdp);
  426. out:
  427. gfs2_glock_dq_uninit(&gh);
  428. return error;
  429. }
  430. static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
  431. {
  432. int error;
  433. error = pick_formal_ino_1(sdp, inum);
  434. if (error <= 0)
  435. return error;
  436. error = pick_formal_ino_2(sdp, inum);
  437. return error;
  438. }
  439. /**
  440. * create_ok - OK to create a new on-disk inode here?
  441. * @dip: Directory in which dinode is to be created
  442. * @name: Name of new dinode
  443. * @mode:
  444. *
  445. * Returns: errno
  446. */
  447. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  448. unsigned int mode)
  449. {
  450. int error;
  451. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  452. if (error)
  453. return error;
  454. /* Don't create entries in an unlinked directory */
  455. if (!dip->i_inode.i_nlink)
  456. return -EPERM;
  457. error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
  458. switch (error) {
  459. case -ENOENT:
  460. error = 0;
  461. break;
  462. case 0:
  463. return -EEXIST;
  464. default:
  465. return error;
  466. }
  467. if (dip->i_di.di_entries == (u32)-1)
  468. return -EFBIG;
  469. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  470. return -EMLINK;
  471. return 0;
  472. }
  473. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  474. unsigned int *uid, unsigned int *gid)
  475. {
  476. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  477. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  478. if (S_ISDIR(*mode))
  479. *mode |= S_ISUID;
  480. else if (dip->i_inode.i_uid != current->fsuid)
  481. *mode &= ~07111;
  482. *uid = dip->i_inode.i_uid;
  483. } else
  484. *uid = current->fsuid;
  485. if (dip->i_inode.i_mode & S_ISGID) {
  486. if (S_ISDIR(*mode))
  487. *mode |= S_ISGID;
  488. *gid = dip->i_inode.i_gid;
  489. } else
  490. *gid = current->fsgid;
  491. }
  492. static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum,
  493. u64 *generation)
  494. {
  495. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  496. int error;
  497. gfs2_alloc_get(dip);
  498. dip->i_alloc.al_requested = RES_DINODE;
  499. error = gfs2_inplace_reserve(dip);
  500. if (error)
  501. goto out;
  502. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  503. if (error)
  504. goto out_ipreserv;
  505. inum->no_addr = gfs2_alloc_di(dip, generation);
  506. gfs2_trans_end(sdp);
  507. out_ipreserv:
  508. gfs2_inplace_release(dip);
  509. out:
  510. gfs2_alloc_put(dip);
  511. return error;
  512. }
  513. /**
  514. * init_dinode - Fill in a new dinode structure
  515. * @dip: the directory this inode is being created in
  516. * @gl: The glock covering the new inode
  517. * @inum: the inode number
  518. * @mode: the file permissions
  519. * @uid:
  520. * @gid:
  521. *
  522. */
  523. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  524. const struct gfs2_inum_host *inum, unsigned int mode,
  525. unsigned int uid, unsigned int gid,
  526. const u64 *generation, dev_t dev)
  527. {
  528. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  529. struct gfs2_dinode *di;
  530. struct buffer_head *dibh;
  531. dibh = gfs2_meta_new(gl, inum->no_addr);
  532. gfs2_trans_add_bh(gl, dibh, 1);
  533. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  534. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  535. di = (struct gfs2_dinode *)dibh->b_data;
  536. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  537. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  538. di->di_mode = cpu_to_be32(mode);
  539. di->di_uid = cpu_to_be32(uid);
  540. di->di_gid = cpu_to_be32(gid);
  541. di->di_nlink = 0;
  542. di->di_size = 0;
  543. di->di_blocks = cpu_to_be64(1);
  544. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
  545. di->di_major = cpu_to_be32(MAJOR(dev));
  546. di->di_minor = cpu_to_be32(MINOR(dev));
  547. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  548. di->di_generation = cpu_to_be64(*generation);
  549. di->di_flags = 0;
  550. if (S_ISREG(mode)) {
  551. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
  552. gfs2_tune_get(sdp, gt_new_files_jdata))
  553. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  554. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
  555. gfs2_tune_get(sdp, gt_new_files_directio))
  556. di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
  557. } else if (S_ISDIR(mode)) {
  558. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  559. GFS2_DIF_INHERIT_DIRECTIO);
  560. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  561. GFS2_DIF_INHERIT_JDATA);
  562. }
  563. di->__pad1 = 0;
  564. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  565. di->di_height = 0;
  566. di->__pad2 = 0;
  567. di->__pad3 = 0;
  568. di->di_depth = 0;
  569. di->di_entries = 0;
  570. memset(&di->__pad4, 0, sizeof(di->__pad4));
  571. di->di_eattr = 0;
  572. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  573. brelse(dibh);
  574. }
  575. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  576. unsigned int mode, const struct gfs2_inum_host *inum,
  577. const u64 *generation, dev_t dev)
  578. {
  579. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  580. unsigned int uid, gid;
  581. int error;
  582. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  583. gfs2_alloc_get(dip);
  584. error = gfs2_quota_lock(dip, uid, gid);
  585. if (error)
  586. goto out;
  587. error = gfs2_quota_check(dip, uid, gid);
  588. if (error)
  589. goto out_quota;
  590. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  591. if (error)
  592. goto out_quota;
  593. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
  594. gfs2_quota_change(dip, +1, uid, gid);
  595. gfs2_trans_end(sdp);
  596. out_quota:
  597. gfs2_quota_unlock(dip);
  598. out:
  599. gfs2_alloc_put(dip);
  600. return error;
  601. }
  602. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  603. struct gfs2_inode *ip)
  604. {
  605. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  606. struct gfs2_alloc *al;
  607. int alloc_required;
  608. struct buffer_head *dibh;
  609. int error;
  610. al = gfs2_alloc_get(dip);
  611. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  612. if (error)
  613. goto fail;
  614. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  615. if (alloc_required < 0)
  616. goto fail;
  617. if (alloc_required) {
  618. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  619. if (error)
  620. goto fail_quota_locks;
  621. al->al_requested = sdp->sd_max_dirres;
  622. error = gfs2_inplace_reserve(dip);
  623. if (error)
  624. goto fail_quota_locks;
  625. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  626. al->al_rgd->rd_ri.ri_length +
  627. 2 * RES_DINODE +
  628. RES_STATFS + RES_QUOTA, 0);
  629. if (error)
  630. goto fail_ipreserv;
  631. } else {
  632. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  633. if (error)
  634. goto fail_quota_locks;
  635. }
  636. error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode));
  637. if (error)
  638. goto fail_end_trans;
  639. error = gfs2_meta_inode_buffer(ip, &dibh);
  640. if (error)
  641. goto fail_end_trans;
  642. ip->i_inode.i_nlink = 1;
  643. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  644. gfs2_dinode_out(ip, dibh->b_data);
  645. brelse(dibh);
  646. return 0;
  647. fail_end_trans:
  648. gfs2_trans_end(sdp);
  649. fail_ipreserv:
  650. if (dip->i_alloc.al_rgd)
  651. gfs2_inplace_release(dip);
  652. fail_quota_locks:
  653. gfs2_quota_unlock(dip);
  654. fail:
  655. gfs2_alloc_put(dip);
  656. return error;
  657. }
  658. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  659. {
  660. int err;
  661. size_t len;
  662. void *value;
  663. char *name;
  664. struct gfs2_ea_request er;
  665. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  666. &name, &value, &len);
  667. if (err) {
  668. if (err == -EOPNOTSUPP)
  669. return 0;
  670. return err;
  671. }
  672. memset(&er, 0, sizeof(struct gfs2_ea_request));
  673. er.er_type = GFS2_EATYPE_SECURITY;
  674. er.er_name = name;
  675. er.er_data = value;
  676. er.er_name_len = strlen(name);
  677. er.er_data_len = len;
  678. err = gfs2_ea_set_i(ip, &er);
  679. kfree(value);
  680. kfree(name);
  681. return err;
  682. }
  683. /**
  684. * gfs2_createi - Create a new inode
  685. * @ghs: An array of two holders
  686. * @name: The name of the new file
  687. * @mode: the permissions on the new inode
  688. *
  689. * @ghs[0] is an initialized holder for the directory
  690. * @ghs[1] is the holder for the inode lock
  691. *
  692. * If the return value is not NULL, the glocks on both the directory and the new
  693. * file are held. A transaction has been started and an inplace reservation
  694. * is held, as well.
  695. *
  696. * Returns: An inode
  697. */
  698. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  699. unsigned int mode, dev_t dev)
  700. {
  701. struct inode *inode;
  702. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  703. struct inode *dir = &dip->i_inode;
  704. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  705. struct gfs2_inum_host inum;
  706. int error;
  707. u64 generation;
  708. if (!name->len || name->len > GFS2_FNAMESIZE)
  709. return ERR_PTR(-ENAMETOOLONG);
  710. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  711. error = gfs2_glock_nq(ghs);
  712. if (error)
  713. goto fail;
  714. error = create_ok(dip, name, mode);
  715. if (error)
  716. goto fail_gunlock;
  717. error = pick_formal_ino(sdp, &inum.no_formal_ino);
  718. if (error)
  719. goto fail_gunlock;
  720. error = alloc_dinode(dip, &inum, &generation);
  721. if (error)
  722. goto fail_gunlock;
  723. if (inum.no_addr < dip->i_num.no_addr) {
  724. gfs2_glock_dq(ghs);
  725. error = gfs2_glock_nq_num(sdp, inum.no_addr,
  726. &gfs2_inode_glops, LM_ST_EXCLUSIVE,
  727. GL_SKIP, ghs + 1);
  728. if (error) {
  729. return ERR_PTR(error);
  730. }
  731. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  732. error = gfs2_glock_nq(ghs);
  733. if (error) {
  734. gfs2_glock_dq_uninit(ghs + 1);
  735. return ERR_PTR(error);
  736. }
  737. error = create_ok(dip, name, mode);
  738. if (error)
  739. goto fail_gunlock2;
  740. } else {
  741. error = gfs2_glock_nq_num(sdp, inum.no_addr,
  742. &gfs2_inode_glops, LM_ST_EXCLUSIVE,
  743. GL_SKIP, ghs + 1);
  744. if (error)
  745. goto fail_gunlock;
  746. }
  747. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
  748. if (error)
  749. goto fail_gunlock2;
  750. inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
  751. if (IS_ERR(inode))
  752. goto fail_gunlock2;
  753. error = gfs2_inode_refresh(GFS2_I(inode));
  754. if (error)
  755. goto fail_iput;
  756. error = gfs2_acl_create(dip, GFS2_I(inode));
  757. if (error)
  758. goto fail_iput;
  759. error = gfs2_security_init(dip, GFS2_I(inode));
  760. if (error)
  761. goto fail_iput;
  762. error = link_dinode(dip, name, GFS2_I(inode));
  763. if (error)
  764. goto fail_iput;
  765. if (!inode)
  766. return ERR_PTR(-ENOMEM);
  767. return inode;
  768. fail_iput:
  769. iput(inode);
  770. fail_gunlock2:
  771. gfs2_glock_dq_uninit(ghs + 1);
  772. fail_gunlock:
  773. gfs2_glock_dq(ghs);
  774. fail:
  775. return ERR_PTR(error);
  776. }
  777. /**
  778. * gfs2_rmdiri - Remove a directory
  779. * @dip: The parent directory of the directory to be removed
  780. * @name: The name of the directory to be removed
  781. * @ip: The GFS2 inode of the directory to be removed
  782. *
  783. * Assumes Glocks on dip and ip are held
  784. *
  785. * Returns: errno
  786. */
  787. int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  788. struct gfs2_inode *ip)
  789. {
  790. struct qstr dotname;
  791. int error;
  792. if (ip->i_di.di_entries != 2) {
  793. if (gfs2_consist_inode(ip))
  794. gfs2_dinode_print(ip);
  795. return -EIO;
  796. }
  797. error = gfs2_dir_del(dip, name);
  798. if (error)
  799. return error;
  800. error = gfs2_change_nlink(dip, -1);
  801. if (error)
  802. return error;
  803. gfs2_str2qstr(&dotname, ".");
  804. error = gfs2_dir_del(ip, &dotname);
  805. if (error)
  806. return error;
  807. gfs2_str2qstr(&dotname, "..");
  808. error = gfs2_dir_del(ip, &dotname);
  809. if (error)
  810. return error;
  811. /* It looks odd, but it really should be done twice */
  812. error = gfs2_change_nlink(ip, -1);
  813. if (error)
  814. return error;
  815. error = gfs2_change_nlink(ip, -1);
  816. if (error)
  817. return error;
  818. return error;
  819. }
  820. /*
  821. * gfs2_unlink_ok - check to see that a inode is still in a directory
  822. * @dip: the directory
  823. * @name: the name of the file
  824. * @ip: the inode
  825. *
  826. * Assumes that the lock on (at least) @dip is held.
  827. *
  828. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  829. */
  830. int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  831. struct gfs2_inode *ip)
  832. {
  833. struct gfs2_inum_host inum;
  834. unsigned int type;
  835. int error;
  836. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  837. return -EPERM;
  838. if ((dip->i_inode.i_mode & S_ISVTX) &&
  839. dip->i_inode.i_uid != current->fsuid &&
  840. ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
  841. return -EPERM;
  842. if (IS_APPEND(&dip->i_inode))
  843. return -EPERM;
  844. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  845. if (error)
  846. return error;
  847. error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
  848. if (error)
  849. return error;
  850. if (!gfs2_inum_equal(&inum, &ip->i_num))
  851. return -ENOENT;
  852. if (IF2DT(ip->i_inode.i_mode) != type) {
  853. gfs2_consist_inode(dip);
  854. return -EIO;
  855. }
  856. return 0;
  857. }
  858. /*
  859. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  860. * @this: move this
  861. * @to: to here
  862. *
  863. * Follow @to back to the root and make sure we don't encounter @this
  864. * Assumes we already hold the rename lock.
  865. *
  866. * Returns: errno
  867. */
  868. int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  869. {
  870. struct inode *dir = &to->i_inode;
  871. struct super_block *sb = dir->i_sb;
  872. struct inode *tmp;
  873. struct qstr dotdot;
  874. int error = 0;
  875. gfs2_str2qstr(&dotdot, "..");
  876. igrab(dir);
  877. for (;;) {
  878. if (dir == &this->i_inode) {
  879. error = -EINVAL;
  880. break;
  881. }
  882. if (dir == sb->s_root->d_inode) {
  883. error = 0;
  884. break;
  885. }
  886. tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
  887. if (IS_ERR(tmp)) {
  888. error = PTR_ERR(tmp);
  889. break;
  890. }
  891. iput(dir);
  892. dir = tmp;
  893. }
  894. iput(dir);
  895. return error;
  896. }
  897. /**
  898. * gfs2_readlinki - return the contents of a symlink
  899. * @ip: the symlink's inode
  900. * @buf: a pointer to the buffer to be filled
  901. * @len: a pointer to the length of @buf
  902. *
  903. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  904. * to be freed by the caller.
  905. *
  906. * Returns: errno
  907. */
  908. int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  909. {
  910. struct gfs2_holder i_gh;
  911. struct buffer_head *dibh;
  912. unsigned int x;
  913. int error;
  914. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  915. error = gfs2_glock_nq_atime(&i_gh);
  916. if (error) {
  917. gfs2_holder_uninit(&i_gh);
  918. return error;
  919. }
  920. if (!ip->i_di.di_size) {
  921. gfs2_consist_inode(ip);
  922. error = -EIO;
  923. goto out;
  924. }
  925. error = gfs2_meta_inode_buffer(ip, &dibh);
  926. if (error)
  927. goto out;
  928. x = ip->i_di.di_size + 1;
  929. if (x > *len) {
  930. *buf = kmalloc(x, GFP_KERNEL);
  931. if (!*buf) {
  932. error = -ENOMEM;
  933. goto out_brelse;
  934. }
  935. }
  936. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  937. *len = x;
  938. out_brelse:
  939. brelse(dibh);
  940. out:
  941. gfs2_glock_dq_uninit(&i_gh);
  942. return error;
  943. }
  944. /**
  945. * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
  946. * conditionally update the inode's atime
  947. * @gh: the holder to acquire
  948. *
  949. * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
  950. * Update if the difference between the current time and the inode's current
  951. * atime is greater than an interval specified at mount.
  952. *
  953. * Returns: errno
  954. */
  955. int gfs2_glock_nq_atime(struct gfs2_holder *gh)
  956. {
  957. struct gfs2_glock *gl = gh->gh_gl;
  958. struct gfs2_sbd *sdp = gl->gl_sbd;
  959. struct gfs2_inode *ip = gl->gl_object;
  960. s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
  961. unsigned int state;
  962. int flags;
  963. int error;
  964. if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
  965. gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
  966. gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
  967. return -EINVAL;
  968. state = gh->gh_state;
  969. flags = gh->gh_flags;
  970. error = gfs2_glock_nq(gh);
  971. if (error)
  972. return error;
  973. if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
  974. (sdp->sd_vfs->s_flags & MS_RDONLY))
  975. return 0;
  976. curtime = get_seconds();
  977. if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
  978. gfs2_glock_dq(gh);
  979. gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
  980. gh);
  981. error = gfs2_glock_nq(gh);
  982. if (error)
  983. return error;
  984. /* Verify that atime hasn't been updated while we were
  985. trying to get exclusive lock. */
  986. curtime = get_seconds();
  987. if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
  988. struct buffer_head *dibh;
  989. struct gfs2_dinode *di;
  990. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  991. if (error == -EROFS)
  992. return 0;
  993. if (error)
  994. goto fail;
  995. error = gfs2_meta_inode_buffer(ip, &dibh);
  996. if (error)
  997. goto fail_end_trans;
  998. ip->i_inode.i_atime.tv_sec = curtime;
  999. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1000. di = (struct gfs2_dinode *)dibh->b_data;
  1001. di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  1002. brelse(dibh);
  1003. gfs2_trans_end(sdp);
  1004. }
  1005. /* If someone else has asked for the glock,
  1006. unlock and let them have it. Then reacquire
  1007. in the original state. */
  1008. if (gfs2_glock_is_blocking(gl)) {
  1009. gfs2_glock_dq(gh);
  1010. gfs2_holder_reinit(state, flags, gh);
  1011. return gfs2_glock_nq(gh);
  1012. }
  1013. }
  1014. return 0;
  1015. fail_end_trans:
  1016. gfs2_trans_end(sdp);
  1017. fail:
  1018. gfs2_glock_dq(gh);
  1019. return error;
  1020. }
  1021. static int
  1022. __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1023. {
  1024. struct buffer_head *dibh;
  1025. int error;
  1026. error = gfs2_meta_inode_buffer(ip, &dibh);
  1027. if (!error) {
  1028. error = inode_setattr(&ip->i_inode, attr);
  1029. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1030. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1031. gfs2_dinode_out(ip, dibh->b_data);
  1032. brelse(dibh);
  1033. }
  1034. return error;
  1035. }
  1036. /**
  1037. * gfs2_setattr_simple -
  1038. * @ip:
  1039. * @attr:
  1040. *
  1041. * Called with a reference on the vnode.
  1042. *
  1043. * Returns: errno
  1044. */
  1045. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1046. {
  1047. int error;
  1048. if (current->journal_info)
  1049. return __gfs2_setattr_simple(ip, attr);
  1050. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  1051. if (error)
  1052. return error;
  1053. error = __gfs2_setattr_simple(ip, attr);
  1054. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1055. return error;
  1056. }