inode.c 30 KB

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