inode.c 28 KB

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