inode.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 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/security.h>
  19. #include <linux/time.h>
  20. #include "gfs2.h"
  21. #include "incore.h"
  22. #include "acl.h"
  23. #include "bmap.h"
  24. #include "dir.h"
  25. #include "xattr.h"
  26. #include "glock.h"
  27. #include "glops.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. struct gfs2_inum_range_host {
  36. u64 ir_start;
  37. u64 ir_length;
  38. };
  39. static int iget_test(struct inode *inode, void *opaque)
  40. {
  41. struct gfs2_inode *ip = GFS2_I(inode);
  42. u64 *no_addr = opaque;
  43. if (ip->i_no_addr == *no_addr)
  44. return 1;
  45. return 0;
  46. }
  47. static int iget_set(struct inode *inode, void *opaque)
  48. {
  49. struct gfs2_inode *ip = GFS2_I(inode);
  50. u64 *no_addr = opaque;
  51. inode->i_ino = (unsigned long)*no_addr;
  52. ip->i_no_addr = *no_addr;
  53. return 0;
  54. }
  55. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
  56. {
  57. unsigned long hash = (unsigned long)no_addr;
  58. return ilookup5(sb, hash, iget_test, &no_addr);
  59. }
  60. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  61. {
  62. unsigned long hash = (unsigned long)no_addr;
  63. return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
  64. }
  65. struct gfs2_skip_data {
  66. u64 no_addr;
  67. int skipped;
  68. };
  69. static int iget_skip_test(struct inode *inode, void *opaque)
  70. {
  71. struct gfs2_inode *ip = GFS2_I(inode);
  72. struct gfs2_skip_data *data = opaque;
  73. if (ip->i_no_addr == data->no_addr) {
  74. if (inode->i_state & (I_FREEING|I_WILL_FREE)){
  75. data->skipped = 1;
  76. return 0;
  77. }
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static int iget_skip_set(struct inode *inode, void *opaque)
  83. {
  84. struct gfs2_inode *ip = GFS2_I(inode);
  85. struct gfs2_skip_data *data = opaque;
  86. if (data->skipped)
  87. return 1;
  88. inode->i_ino = (unsigned long)(data->no_addr);
  89. ip->i_no_addr = data->no_addr;
  90. return 0;
  91. }
  92. static struct inode *gfs2_iget_skip(struct super_block *sb,
  93. u64 no_addr)
  94. {
  95. struct gfs2_skip_data data;
  96. unsigned long hash = (unsigned long)no_addr;
  97. data.no_addr = no_addr;
  98. data.skipped = 0;
  99. return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data);
  100. }
  101. /**
  102. * GFS2 lookup code fills in vfs inode contents based on info obtained
  103. * from directory entry inside gfs2_inode_lookup(). This has caused issues
  104. * with NFS code path since its get_dentry routine doesn't have the relevant
  105. * directory entry when gfs2_inode_lookup() is invoked. Part of the code
  106. * segment inside gfs2_inode_lookup code needs to get moved around.
  107. *
  108. * Clears I_NEW as well.
  109. **/
  110. void gfs2_set_iop(struct inode *inode)
  111. {
  112. struct gfs2_sbd *sdp = GFS2_SB(inode);
  113. umode_t mode = inode->i_mode;
  114. if (S_ISREG(mode)) {
  115. inode->i_op = &gfs2_file_iops;
  116. if (gfs2_localflocks(sdp))
  117. inode->i_fop = &gfs2_file_fops_nolock;
  118. else
  119. inode->i_fop = &gfs2_file_fops;
  120. } else if (S_ISDIR(mode)) {
  121. inode->i_op = &gfs2_dir_iops;
  122. if (gfs2_localflocks(sdp))
  123. inode->i_fop = &gfs2_dir_fops_nolock;
  124. else
  125. inode->i_fop = &gfs2_dir_fops;
  126. } else if (S_ISLNK(mode)) {
  127. inode->i_op = &gfs2_symlink_iops;
  128. } else {
  129. inode->i_op = &gfs2_file_iops;
  130. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  131. }
  132. unlock_new_inode(inode);
  133. }
  134. /**
  135. * gfs2_inode_lookup - Lookup an inode
  136. * @sb: The super block
  137. * @no_addr: The inode number
  138. * @type: The type of the inode
  139. *
  140. * Returns: A VFS inode, or an error
  141. */
  142. struct inode *gfs2_inode_lookup(struct super_block *sb,
  143. unsigned int type,
  144. u64 no_addr,
  145. u64 no_formal_ino)
  146. {
  147. struct inode *inode;
  148. struct gfs2_inode *ip;
  149. struct gfs2_glock *io_gl = NULL;
  150. int error;
  151. inode = gfs2_iget(sb, no_addr);
  152. ip = GFS2_I(inode);
  153. if (!inode)
  154. return ERR_PTR(-ENOBUFS);
  155. if (inode->i_state & I_NEW) {
  156. struct gfs2_sbd *sdp = GFS2_SB(inode);
  157. ip->i_no_formal_ino = no_formal_ino;
  158. error = gfs2_glock_get(sdp, 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, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  163. if (unlikely(error))
  164. goto fail_put;
  165. set_bit(GIF_INVALID, &ip->i_flags);
  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. ip->i_iopen_gh.gh_gl->gl_object = ip;
  170. gfs2_glock_put(io_gl);
  171. io_gl = NULL;
  172. if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
  173. goto gfs2_nfsbypass;
  174. inode->i_mode = DT2IF(type);
  175. /*
  176. * We must read the inode in order to work out its type in
  177. * this case. Note that this doesn't happen often as we normally
  178. * know the type beforehand. This code path only occurs during
  179. * unlinked inode recovery (where it is safe to do this glock,
  180. * which is not true in the general case).
  181. */
  182. if (type == DT_UNKNOWN) {
  183. struct gfs2_holder gh;
  184. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  185. if (unlikely(error))
  186. goto fail_glock;
  187. /* Inode is now uptodate */
  188. gfs2_glock_dq_uninit(&gh);
  189. }
  190. gfs2_set_iop(inode);
  191. }
  192. gfs2_nfsbypass:
  193. return inode;
  194. fail_glock:
  195. gfs2_glock_dq(&ip->i_iopen_gh);
  196. fail_iopen:
  197. if (io_gl)
  198. gfs2_glock_put(io_gl);
  199. fail_put:
  200. if (inode->i_state & I_NEW)
  201. ip->i_gl->gl_object = NULL;
  202. gfs2_glock_put(ip->i_gl);
  203. fail:
  204. if (inode->i_state & I_NEW)
  205. iget_failed(inode);
  206. else
  207. iput(inode);
  208. return ERR_PTR(error);
  209. }
  210. /**
  211. * gfs2_process_unlinked_inode - Lookup an unlinked inode for reclamation
  212. * and try to reclaim it by doing iput.
  213. *
  214. * This function assumes no rgrp locks are currently held.
  215. *
  216. * @sb: The super block
  217. * no_addr: The inode number
  218. *
  219. */
  220. void gfs2_process_unlinked_inode(struct super_block *sb, u64 no_addr)
  221. {
  222. struct gfs2_sbd *sdp;
  223. struct gfs2_inode *ip;
  224. struct gfs2_glock *io_gl = NULL;
  225. int error;
  226. struct gfs2_holder gh;
  227. struct inode *inode;
  228. inode = gfs2_iget_skip(sb, no_addr);
  229. if (!inode)
  230. return;
  231. /* If it's not a new inode, someone's using it, so leave it alone. */
  232. if (!(inode->i_state & I_NEW)) {
  233. iput(inode);
  234. return;
  235. }
  236. ip = GFS2_I(inode);
  237. sdp = GFS2_SB(inode);
  238. ip->i_no_formal_ino = -1;
  239. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  240. if (unlikely(error))
  241. goto fail;
  242. ip->i_gl->gl_object = ip;
  243. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  244. if (unlikely(error))
  245. goto fail_put;
  246. set_bit(GIF_INVALID, &ip->i_flags);
  247. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, LM_FLAG_TRY | GL_EXACT,
  248. &ip->i_iopen_gh);
  249. if (unlikely(error))
  250. goto fail_iopen;
  251. ip->i_iopen_gh.gh_gl->gl_object = ip;
  252. gfs2_glock_put(io_gl);
  253. io_gl = NULL;
  254. inode->i_mode = DT2IF(DT_UNKNOWN);
  255. /*
  256. * We must read the inode in order to work out its type in
  257. * this case. Note that this doesn't happen often as we normally
  258. * know the type beforehand. This code path only occurs during
  259. * unlinked inode recovery (where it is safe to do this glock,
  260. * which is not true in the general case).
  261. */
  262. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY,
  263. &gh);
  264. if (unlikely(error))
  265. goto fail_glock;
  266. /* Inode is now uptodate */
  267. gfs2_glock_dq_uninit(&gh);
  268. gfs2_set_iop(inode);
  269. /* The iput will cause it to be deleted. */
  270. iput(inode);
  271. return;
  272. fail_glock:
  273. gfs2_glock_dq(&ip->i_iopen_gh);
  274. fail_iopen:
  275. if (io_gl)
  276. gfs2_glock_put(io_gl);
  277. fail_put:
  278. ip->i_gl->gl_object = NULL;
  279. gfs2_glock_put(ip->i_gl);
  280. fail:
  281. iget_failed(inode);
  282. return;
  283. }
  284. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  285. {
  286. const struct gfs2_dinode *str = buf;
  287. struct timespec atime;
  288. u16 height, depth;
  289. if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
  290. goto corrupt;
  291. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  292. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  293. ip->i_inode.i_rdev = 0;
  294. switch (ip->i_inode.i_mode & S_IFMT) {
  295. case S_IFBLK:
  296. case S_IFCHR:
  297. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  298. be32_to_cpu(str->di_minor));
  299. break;
  300. };
  301. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  302. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  303. /*
  304. * We will need to review setting the nlink count here in the
  305. * light of the forthcoming ro bind mount work. This is a reminder
  306. * to do that.
  307. */
  308. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  309. i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
  310. gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
  311. atime.tv_sec = be64_to_cpu(str->di_atime);
  312. atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  313. if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
  314. ip->i_inode.i_atime = atime;
  315. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  316. ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
  317. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  318. ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
  319. ip->i_goal = be64_to_cpu(str->di_goal_meta);
  320. ip->i_generation = be64_to_cpu(str->di_generation);
  321. ip->i_diskflags = be32_to_cpu(str->di_flags);
  322. gfs2_set_inode_flags(&ip->i_inode);
  323. height = be16_to_cpu(str->di_height);
  324. if (unlikely(height > GFS2_MAX_META_HEIGHT))
  325. goto corrupt;
  326. ip->i_height = (u8)height;
  327. depth = be16_to_cpu(str->di_depth);
  328. if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
  329. goto corrupt;
  330. ip->i_depth = (u8)depth;
  331. ip->i_entries = be32_to_cpu(str->di_entries);
  332. ip->i_eattr = be64_to_cpu(str->di_eattr);
  333. if (S_ISREG(ip->i_inode.i_mode))
  334. gfs2_set_aops(&ip->i_inode);
  335. return 0;
  336. corrupt:
  337. if (gfs2_consist_inode(ip))
  338. gfs2_dinode_print(ip);
  339. return -EIO;
  340. }
  341. /**
  342. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  343. * @ip: The GFS2 inode
  344. *
  345. * Returns: errno
  346. */
  347. int gfs2_inode_refresh(struct gfs2_inode *ip)
  348. {
  349. struct buffer_head *dibh;
  350. int error;
  351. error = gfs2_meta_inode_buffer(ip, &dibh);
  352. if (error)
  353. return error;
  354. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  355. brelse(dibh);
  356. return -EIO;
  357. }
  358. error = gfs2_dinode_in(ip, dibh->b_data);
  359. brelse(dibh);
  360. clear_bit(GIF_INVALID, &ip->i_flags);
  361. return error;
  362. }
  363. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  364. {
  365. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  366. struct gfs2_alloc *al;
  367. struct gfs2_rgrpd *rgd;
  368. int error;
  369. if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
  370. if (gfs2_consist_inode(ip))
  371. gfs2_dinode_print(ip);
  372. return -EIO;
  373. }
  374. al = gfs2_alloc_get(ip);
  375. if (!al)
  376. return -ENOMEM;
  377. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  378. if (error)
  379. goto out;
  380. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  381. if (error)
  382. goto out_qs;
  383. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  384. if (!rgd) {
  385. gfs2_consist_inode(ip);
  386. error = -EIO;
  387. goto out_rindex_relse;
  388. }
  389. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  390. &al->al_rgd_gh);
  391. if (error)
  392. goto out_rindex_relse;
  393. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  394. if (error)
  395. goto out_rg_gunlock;
  396. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  397. set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
  398. gfs2_free_di(rgd, ip);
  399. gfs2_trans_end(sdp);
  400. out_rg_gunlock:
  401. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  402. out_rindex_relse:
  403. gfs2_glock_dq_uninit(&al->al_ri_gh);
  404. out_qs:
  405. gfs2_quota_unhold(ip);
  406. out:
  407. gfs2_alloc_put(ip);
  408. return error;
  409. }
  410. /**
  411. * gfs2_change_nlink - Change nlink count on inode
  412. * @ip: The GFS2 inode
  413. * @diff: The change in the nlink count required
  414. *
  415. * Returns: errno
  416. */
  417. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  418. {
  419. struct buffer_head *dibh;
  420. u32 nlink;
  421. int error;
  422. BUG_ON(diff != 1 && diff != -1);
  423. nlink = ip->i_inode.i_nlink + diff;
  424. /* If we are reducing the nlink count, but the new value ends up being
  425. bigger than the old one, we must have underflowed. */
  426. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  427. if (gfs2_consist_inode(ip))
  428. gfs2_dinode_print(ip);
  429. return -EIO;
  430. }
  431. error = gfs2_meta_inode_buffer(ip, &dibh);
  432. if (error)
  433. return error;
  434. if (diff > 0)
  435. inc_nlink(&ip->i_inode);
  436. else
  437. drop_nlink(&ip->i_inode);
  438. ip->i_inode.i_ctime = CURRENT_TIME;
  439. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  440. gfs2_dinode_out(ip, dibh->b_data);
  441. brelse(dibh);
  442. mark_inode_dirty(&ip->i_inode);
  443. if (ip->i_inode.i_nlink == 0)
  444. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  445. return error;
  446. }
  447. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  448. {
  449. struct qstr qstr;
  450. struct inode *inode;
  451. gfs2_str2qstr(&qstr, name);
  452. inode = gfs2_lookupi(dip, &qstr, 1);
  453. /* gfs2_lookupi has inconsistent callers: vfs
  454. * related routines expect NULL for no entry found,
  455. * gfs2_lookup_simple callers expect ENOENT
  456. * and do not check for NULL.
  457. */
  458. if (inode == NULL)
  459. return ERR_PTR(-ENOENT);
  460. else
  461. return inode;
  462. }
  463. /**
  464. * gfs2_lookupi - Look up a filename in a directory and return its inode
  465. * @d_gh: An initialized holder for the directory glock
  466. * @name: The name of the inode to look for
  467. * @is_root: If 1, ignore the caller's permissions
  468. * @i_gh: An uninitialized holder for the new inode glock
  469. *
  470. * This can be called via the VFS filldir function when NFS is doing
  471. * a readdirplus and the inode which its intending to stat isn't
  472. * already in cache. In this case we must not take the directory glock
  473. * again, since the readdir call will have already taken that lock.
  474. *
  475. * Returns: errno
  476. */
  477. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  478. int is_root)
  479. {
  480. struct super_block *sb = dir->i_sb;
  481. struct gfs2_inode *dip = GFS2_I(dir);
  482. struct gfs2_holder d_gh;
  483. int error = 0;
  484. struct inode *inode = NULL;
  485. int unlock = 0;
  486. if (!name->len || name->len > GFS2_FNAMESIZE)
  487. return ERR_PTR(-ENAMETOOLONG);
  488. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  489. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  490. dir == sb->s_root->d_inode)) {
  491. igrab(dir);
  492. return dir;
  493. }
  494. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  495. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  496. if (error)
  497. return ERR_PTR(error);
  498. unlock = 1;
  499. }
  500. if (!is_root) {
  501. error = gfs2_permission(dir, MAY_EXEC);
  502. if (error)
  503. goto out;
  504. }
  505. inode = gfs2_dir_search(dir, name);
  506. if (IS_ERR(inode))
  507. error = PTR_ERR(inode);
  508. out:
  509. if (unlock)
  510. gfs2_glock_dq_uninit(&d_gh);
  511. if (error == -ENOENT)
  512. return NULL;
  513. return inode ? inode : ERR_PTR(error);
  514. }
  515. /**
  516. * create_ok - OK to create a new on-disk inode here?
  517. * @dip: Directory in which dinode is to be created
  518. * @name: Name of new dinode
  519. * @mode:
  520. *
  521. * Returns: errno
  522. */
  523. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  524. unsigned int mode)
  525. {
  526. int error;
  527. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  528. if (error)
  529. return error;
  530. /* Don't create entries in an unlinked directory */
  531. if (!dip->i_inode.i_nlink)
  532. return -EPERM;
  533. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  534. switch (error) {
  535. case -ENOENT:
  536. error = 0;
  537. break;
  538. case 0:
  539. return -EEXIST;
  540. default:
  541. return error;
  542. }
  543. if (dip->i_entries == (u32)-1)
  544. return -EFBIG;
  545. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  546. return -EMLINK;
  547. return 0;
  548. }
  549. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  550. unsigned int *uid, unsigned int *gid)
  551. {
  552. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  553. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  554. if (S_ISDIR(*mode))
  555. *mode |= S_ISUID;
  556. else if (dip->i_inode.i_uid != current_fsuid())
  557. *mode &= ~07111;
  558. *uid = dip->i_inode.i_uid;
  559. } else
  560. *uid = current_fsuid();
  561. if (dip->i_inode.i_mode & S_ISGID) {
  562. if (S_ISDIR(*mode))
  563. *mode |= S_ISGID;
  564. *gid = dip->i_inode.i_gid;
  565. } else
  566. *gid = current_fsgid();
  567. }
  568. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  569. {
  570. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  571. int error;
  572. if (gfs2_alloc_get(dip) == NULL)
  573. return -ENOMEM;
  574. dip->i_alloc->al_requested = RES_DINODE;
  575. error = gfs2_inplace_reserve(dip);
  576. if (error)
  577. goto out;
  578. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  579. if (error)
  580. goto out_ipreserv;
  581. error = gfs2_alloc_di(dip, no_addr, generation);
  582. gfs2_trans_end(sdp);
  583. out_ipreserv:
  584. gfs2_inplace_release(dip);
  585. out:
  586. gfs2_alloc_put(dip);
  587. return error;
  588. }
  589. /**
  590. * init_dinode - Fill in a new dinode structure
  591. * @dip: the directory this inode is being created in
  592. * @gl: The glock covering the new inode
  593. * @inum: the inode number
  594. * @mode: the file permissions
  595. * @uid:
  596. * @gid:
  597. *
  598. */
  599. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  600. const struct gfs2_inum_host *inum, unsigned int mode,
  601. unsigned int uid, unsigned int gid,
  602. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  603. {
  604. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  605. struct gfs2_dinode *di;
  606. struct buffer_head *dibh;
  607. struct timespec tv = CURRENT_TIME;
  608. dibh = gfs2_meta_new(gl, inum->no_addr);
  609. gfs2_trans_add_bh(gl, dibh, 1);
  610. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  611. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  612. di = (struct gfs2_dinode *)dibh->b_data;
  613. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  614. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  615. di->di_mode = cpu_to_be32(mode);
  616. di->di_uid = cpu_to_be32(uid);
  617. di->di_gid = cpu_to_be32(gid);
  618. di->di_nlink = 0;
  619. di->di_size = 0;
  620. di->di_blocks = cpu_to_be64(1);
  621. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  622. di->di_major = cpu_to_be32(MAJOR(dev));
  623. di->di_minor = cpu_to_be32(MINOR(dev));
  624. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  625. di->di_generation = cpu_to_be64(*generation);
  626. di->di_flags = 0;
  627. if (S_ISREG(mode)) {
  628. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  629. gfs2_tune_get(sdp, gt_new_files_jdata))
  630. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  631. } else if (S_ISDIR(mode)) {
  632. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  633. GFS2_DIF_INHERIT_JDATA);
  634. }
  635. di->__pad1 = 0;
  636. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  637. di->di_height = 0;
  638. di->__pad2 = 0;
  639. di->__pad3 = 0;
  640. di->di_depth = 0;
  641. di->di_entries = 0;
  642. memset(&di->__pad4, 0, sizeof(di->__pad4));
  643. di->di_eattr = 0;
  644. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  645. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  646. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  647. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  648. set_buffer_uptodate(dibh);
  649. *bhp = dibh;
  650. }
  651. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  652. unsigned int mode, const struct gfs2_inum_host *inum,
  653. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  654. {
  655. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  656. unsigned int uid, gid;
  657. int error;
  658. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  659. if (!gfs2_alloc_get(dip))
  660. return -ENOMEM;
  661. error = gfs2_quota_lock(dip, uid, gid);
  662. if (error)
  663. goto out;
  664. error = gfs2_quota_check(dip, uid, gid);
  665. if (error)
  666. goto out_quota;
  667. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  668. if (error)
  669. goto out_quota;
  670. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
  671. gfs2_quota_change(dip, +1, uid, gid);
  672. gfs2_trans_end(sdp);
  673. out_quota:
  674. gfs2_quota_unlock(dip);
  675. out:
  676. gfs2_alloc_put(dip);
  677. return error;
  678. }
  679. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  680. struct gfs2_inode *ip)
  681. {
  682. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  683. struct gfs2_alloc *al;
  684. int alloc_required;
  685. struct buffer_head *dibh;
  686. int error;
  687. al = gfs2_alloc_get(dip);
  688. if (!al)
  689. return -ENOMEM;
  690. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  691. if (error)
  692. goto fail;
  693. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  694. if (alloc_required < 0)
  695. goto fail_quota_locks;
  696. if (alloc_required) {
  697. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  698. if (error)
  699. goto fail_quota_locks;
  700. al->al_requested = sdp->sd_max_dirres;
  701. error = gfs2_inplace_reserve(dip);
  702. if (error)
  703. goto fail_quota_locks;
  704. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  705. al->al_rgd->rd_length +
  706. 2 * RES_DINODE +
  707. RES_STATFS + RES_QUOTA, 0);
  708. if (error)
  709. goto fail_ipreserv;
  710. } else {
  711. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  712. if (error)
  713. goto fail_quota_locks;
  714. }
  715. error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
  716. if (error)
  717. goto fail_end_trans;
  718. error = gfs2_meta_inode_buffer(ip, &dibh);
  719. if (error)
  720. goto fail_end_trans;
  721. ip->i_inode.i_nlink = 1;
  722. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  723. gfs2_dinode_out(ip, dibh->b_data);
  724. brelse(dibh);
  725. return 0;
  726. fail_end_trans:
  727. gfs2_trans_end(sdp);
  728. fail_ipreserv:
  729. if (dip->i_alloc->al_rgd)
  730. gfs2_inplace_release(dip);
  731. fail_quota_locks:
  732. gfs2_quota_unlock(dip);
  733. fail:
  734. gfs2_alloc_put(dip);
  735. return error;
  736. }
  737. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  738. {
  739. int err;
  740. size_t len;
  741. void *value;
  742. char *name;
  743. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  744. &name, &value, &len);
  745. if (err) {
  746. if (err == -EOPNOTSUPP)
  747. return 0;
  748. return err;
  749. }
  750. err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
  751. GFS2_EATYPE_SECURITY);
  752. kfree(value);
  753. kfree(name);
  754. return err;
  755. }
  756. /**
  757. * gfs2_createi - Create a new inode
  758. * @ghs: An array of two holders
  759. * @name: The name of the new file
  760. * @mode: the permissions on the new inode
  761. *
  762. * @ghs[0] is an initialized holder for the directory
  763. * @ghs[1] is the holder for the inode lock
  764. *
  765. * If the return value is not NULL, the glocks on both the directory and the new
  766. * file are held. A transaction has been started and an inplace reservation
  767. * is held, as well.
  768. *
  769. * Returns: An inode
  770. */
  771. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  772. unsigned int mode, dev_t dev)
  773. {
  774. struct inode *inode = NULL;
  775. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  776. struct inode *dir = &dip->i_inode;
  777. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  778. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  779. int error;
  780. u64 generation;
  781. struct buffer_head *bh = NULL;
  782. if (!name->len || name->len > GFS2_FNAMESIZE)
  783. return ERR_PTR(-ENAMETOOLONG);
  784. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  785. error = gfs2_glock_nq(ghs);
  786. if (error)
  787. goto fail;
  788. error = create_ok(dip, name, mode);
  789. if (error)
  790. goto fail_gunlock;
  791. error = alloc_dinode(dip, &inum.no_addr, &generation);
  792. if (error)
  793. goto fail_gunlock;
  794. inum.no_formal_ino = generation;
  795. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  796. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  797. if (error)
  798. goto fail_gunlock;
  799. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
  800. if (error)
  801. goto fail_gunlock2;
  802. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  803. inum.no_formal_ino);
  804. if (IS_ERR(inode))
  805. goto fail_gunlock2;
  806. error = gfs2_inode_refresh(GFS2_I(inode));
  807. if (error)
  808. goto fail_gunlock2;
  809. error = gfs2_acl_create(dip, inode);
  810. if (error)
  811. goto fail_gunlock2;
  812. error = gfs2_security_init(dip, GFS2_I(inode));
  813. if (error)
  814. goto fail_gunlock2;
  815. error = link_dinode(dip, name, GFS2_I(inode));
  816. if (error)
  817. goto fail_gunlock2;
  818. if (bh)
  819. brelse(bh);
  820. return inode;
  821. fail_gunlock2:
  822. gfs2_glock_dq_uninit(ghs + 1);
  823. if (inode && !IS_ERR(inode))
  824. iput(inode);
  825. fail_gunlock:
  826. gfs2_glock_dq(ghs);
  827. fail:
  828. if (bh)
  829. brelse(bh);
  830. return ERR_PTR(error);
  831. }
  832. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  833. {
  834. struct inode *inode = &ip->i_inode;
  835. struct buffer_head *dibh;
  836. int error;
  837. error = gfs2_meta_inode_buffer(ip, &dibh);
  838. if (error)
  839. return error;
  840. if ((attr->ia_valid & ATTR_SIZE) &&
  841. attr->ia_size != i_size_read(inode)) {
  842. error = vmtruncate(inode, attr->ia_size);
  843. if (error)
  844. return error;
  845. }
  846. setattr_copy(inode, attr);
  847. mark_inode_dirty(inode);
  848. gfs2_assert_warn(GFS2_SB(inode), !error);
  849. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  850. gfs2_dinode_out(ip, dibh->b_data);
  851. brelse(dibh);
  852. return 0;
  853. }
  854. /**
  855. * gfs2_setattr_simple -
  856. * @ip:
  857. * @attr:
  858. *
  859. * Called with a reference on the vnode.
  860. *
  861. * Returns: errno
  862. */
  863. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  864. {
  865. int error;
  866. if (current->journal_info)
  867. return __gfs2_setattr_simple(ip, attr);
  868. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  869. if (error)
  870. return error;
  871. error = __gfs2_setattr_simple(ip, attr);
  872. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  873. return error;
  874. }
  875. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  876. {
  877. struct gfs2_dinode *str = buf;
  878. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  879. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  880. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  881. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  882. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  883. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  884. str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  885. str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  886. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  887. str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
  888. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  889. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  890. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  891. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  892. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  893. str->di_goal_data = cpu_to_be64(ip->i_goal);
  894. str->di_generation = cpu_to_be64(ip->i_generation);
  895. str->di_flags = cpu_to_be32(ip->i_diskflags);
  896. str->di_height = cpu_to_be16(ip->i_height);
  897. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  898. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  899. GFS2_FORMAT_DE : 0);
  900. str->di_depth = cpu_to_be16(ip->i_depth);
  901. str->di_entries = cpu_to_be32(ip->i_entries);
  902. str->di_eattr = cpu_to_be64(ip->i_eattr);
  903. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  904. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  905. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  906. }
  907. void gfs2_dinode_print(const struct gfs2_inode *ip)
  908. {
  909. printk(KERN_INFO " no_formal_ino = %llu\n",
  910. (unsigned long long)ip->i_no_formal_ino);
  911. printk(KERN_INFO " no_addr = %llu\n",
  912. (unsigned long long)ip->i_no_addr);
  913. printk(KERN_INFO " i_size = %llu\n",
  914. (unsigned long long)i_size_read(&ip->i_inode));
  915. printk(KERN_INFO " blocks = %llu\n",
  916. (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
  917. printk(KERN_INFO " i_goal = %llu\n",
  918. (unsigned long long)ip->i_goal);
  919. printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
  920. printk(KERN_INFO " i_height = %u\n", ip->i_height);
  921. printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
  922. printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
  923. printk(KERN_INFO " i_eattr = %llu\n",
  924. (unsigned long long)ip->i_eattr);
  925. }