inode.c 28 KB

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