inode.c 30 KB

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