inode.c 30 KB

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