inode.c 30 KB

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