inode.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/security.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  166. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  167. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  168. fail_iopen:
  169. if (io_gl)
  170. gfs2_glock_put(io_gl);
  171. fail_put:
  172. ip->i_gl->gl_object = NULL;
  173. gfs2_glock_put(ip->i_gl);
  174. fail:
  175. iget_failed(inode);
  176. return ERR_PTR(error);
  177. }
  178. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  179. u64 *no_formal_ino, unsigned int blktype)
  180. {
  181. struct super_block *sb = sdp->sd_vfs;
  182. struct gfs2_holder i_gh;
  183. struct inode *inode = NULL;
  184. int error;
  185. /* Must not read in block until block type is verified */
  186. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  187. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  188. if (error)
  189. return ERR_PTR(error);
  190. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  191. if (error)
  192. goto fail;
  193. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  194. if (IS_ERR(inode))
  195. goto fail;
  196. /* Two extra checks for NFS only */
  197. if (no_formal_ino) {
  198. error = -ESTALE;
  199. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  200. goto fail_iput;
  201. error = -EIO;
  202. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  203. goto fail_iput;
  204. error = 0;
  205. }
  206. fail:
  207. gfs2_glock_dq_uninit(&i_gh);
  208. return error ? ERR_PTR(error) : inode;
  209. fail_iput:
  210. iput(inode);
  211. goto fail;
  212. }
  213. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  214. {
  215. struct qstr qstr;
  216. struct inode *inode;
  217. gfs2_str2qstr(&qstr, name);
  218. inode = gfs2_lookupi(dip, &qstr, 1);
  219. /* gfs2_lookupi has inconsistent callers: vfs
  220. * related routines expect NULL for no entry found,
  221. * gfs2_lookup_simple callers expect ENOENT
  222. * and do not check for NULL.
  223. */
  224. if (inode == NULL)
  225. return ERR_PTR(-ENOENT);
  226. else
  227. return inode;
  228. }
  229. /**
  230. * gfs2_lookupi - Look up a filename in a directory and return its inode
  231. * @d_gh: An initialized holder for the directory glock
  232. * @name: The name of the inode to look for
  233. * @is_root: If 1, ignore the caller's permissions
  234. * @i_gh: An uninitialized holder for the new inode glock
  235. *
  236. * This can be called via the VFS filldir function when NFS is doing
  237. * a readdirplus and the inode which its intending to stat isn't
  238. * already in cache. In this case we must not take the directory glock
  239. * again, since the readdir call will have already taken that lock.
  240. *
  241. * Returns: errno
  242. */
  243. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  244. int is_root)
  245. {
  246. struct super_block *sb = dir->i_sb;
  247. struct gfs2_inode *dip = GFS2_I(dir);
  248. struct gfs2_holder d_gh;
  249. int error = 0;
  250. struct inode *inode = NULL;
  251. int unlock = 0;
  252. if (!name->len || name->len > GFS2_FNAMESIZE)
  253. return ERR_PTR(-ENAMETOOLONG);
  254. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  255. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  256. dir == sb->s_root->d_inode)) {
  257. igrab(dir);
  258. return dir;
  259. }
  260. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  261. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  262. if (error)
  263. return ERR_PTR(error);
  264. unlock = 1;
  265. }
  266. if (!is_root) {
  267. error = gfs2_permission(dir, MAY_EXEC);
  268. if (error)
  269. goto out;
  270. }
  271. inode = gfs2_dir_search(dir, name);
  272. if (IS_ERR(inode))
  273. error = PTR_ERR(inode);
  274. out:
  275. if (unlock)
  276. gfs2_glock_dq_uninit(&d_gh);
  277. if (error == -ENOENT)
  278. return NULL;
  279. return inode ? inode : ERR_PTR(error);
  280. }
  281. /**
  282. * create_ok - OK to create a new on-disk inode here?
  283. * @dip: Directory in which dinode is to be created
  284. * @name: Name of new dinode
  285. * @mode:
  286. *
  287. * Returns: errno
  288. */
  289. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  290. umode_t mode)
  291. {
  292. int error;
  293. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  294. if (error)
  295. return error;
  296. /* Don't create entries in an unlinked directory */
  297. if (!dip->i_inode.i_nlink)
  298. return -ENOENT;
  299. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  300. switch (error) {
  301. case -ENOENT:
  302. error = 0;
  303. break;
  304. case 0:
  305. return -EEXIST;
  306. default:
  307. return error;
  308. }
  309. if (dip->i_entries == (u32)-1)
  310. return -EFBIG;
  311. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  312. return -EMLINK;
  313. return 0;
  314. }
  315. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  316. struct inode *inode)
  317. {
  318. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  319. (dip->i_inode.i_mode & S_ISUID) &&
  320. !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
  321. if (S_ISDIR(inode->i_mode))
  322. inode->i_mode |= S_ISUID;
  323. else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
  324. inode->i_mode &= ~07111;
  325. inode->i_uid = dip->i_inode.i_uid;
  326. } else
  327. inode->i_uid = current_fsuid();
  328. if (dip->i_inode.i_mode & S_ISGID) {
  329. if (S_ISDIR(inode->i_mode))
  330. inode->i_mode |= S_ISGID;
  331. inode->i_gid = dip->i_inode.i_gid;
  332. } else
  333. inode->i_gid = current_fsgid();
  334. }
  335. static int alloc_dinode(struct gfs2_inode *ip, u32 flags)
  336. {
  337. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  338. int error;
  339. int dblocks = 1;
  340. error = gfs2_quota_lock_check(ip);
  341. if (error)
  342. goto out;
  343. error = gfs2_inplace_reserve(ip, RES_DINODE, flags);
  344. if (error)
  345. goto out_quota;
  346. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 0);
  347. if (error)
  348. goto out_ipreserv;
  349. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, &dblocks, 1, &ip->i_generation);
  350. ip->i_no_formal_ino = ip->i_generation;
  351. ip->i_inode.i_ino = ip->i_no_addr;
  352. ip->i_goal = ip->i_no_addr;
  353. gfs2_trans_end(sdp);
  354. out_ipreserv:
  355. gfs2_inplace_release(ip);
  356. out_quota:
  357. gfs2_quota_unlock(ip);
  358. out:
  359. return error;
  360. }
  361. static void gfs2_init_dir(struct buffer_head *dibh,
  362. const struct gfs2_inode *parent)
  363. {
  364. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  365. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  366. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  367. dent->de_inum = di->di_num; /* already GFS2 endian */
  368. dent->de_type = cpu_to_be16(DT_DIR);
  369. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  370. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  371. gfs2_inum_out(parent, dent);
  372. dent->de_type = cpu_to_be16(DT_DIR);
  373. }
  374. /**
  375. * init_dinode - Fill in a new dinode structure
  376. * @dip: The directory this inode is being created in
  377. * @ip: The inode
  378. * @symname: The symlink destination (if a symlink)
  379. * @bhp: The buffer head (returned to caller)
  380. *
  381. */
  382. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  383. const char *symname)
  384. {
  385. struct gfs2_dinode *di;
  386. struct buffer_head *dibh;
  387. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  388. gfs2_trans_add_meta(ip->i_gl, dibh);
  389. di = (struct gfs2_dinode *)dibh->b_data;
  390. gfs2_dinode_out(ip, di);
  391. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  392. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  393. di->__pad1 = 0;
  394. di->__pad2 = 0;
  395. di->__pad3 = 0;
  396. memset(&di->__pad4, 0, sizeof(di->__pad4));
  397. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  398. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  399. switch(ip->i_inode.i_mode & S_IFMT) {
  400. case S_IFDIR:
  401. gfs2_init_dir(dibh, dip);
  402. break;
  403. case S_IFLNK:
  404. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  405. break;
  406. }
  407. set_buffer_uptodate(dibh);
  408. brelse(dibh);
  409. }
  410. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  411. struct gfs2_inode *ip, int arq)
  412. {
  413. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  414. int error;
  415. if (arq) {
  416. error = gfs2_quota_lock_check(dip);
  417. if (error)
  418. goto fail_quota_locks;
  419. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  420. if (error)
  421. goto fail_quota_locks;
  422. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  423. dip->i_rgd->rd_length +
  424. 2 * RES_DINODE +
  425. RES_STATFS + RES_QUOTA, 0);
  426. if (error)
  427. goto fail_ipreserv;
  428. } else {
  429. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  430. if (error)
  431. goto fail_quota_locks;
  432. }
  433. error = gfs2_dir_add(&dip->i_inode, name, ip);
  434. if (error)
  435. goto fail_end_trans;
  436. fail_end_trans:
  437. gfs2_trans_end(sdp);
  438. fail_ipreserv:
  439. gfs2_inplace_release(dip);
  440. fail_quota_locks:
  441. gfs2_quota_unlock(dip);
  442. return error;
  443. }
  444. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  445. void *fs_info)
  446. {
  447. const struct xattr *xattr;
  448. int err = 0;
  449. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  450. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  451. xattr->value_len, 0,
  452. GFS2_EATYPE_SECURITY);
  453. if (err < 0)
  454. break;
  455. }
  456. return err;
  457. }
  458. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  459. const struct qstr *qstr)
  460. {
  461. return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  462. &gfs2_initxattrs, NULL);
  463. }
  464. /**
  465. * gfs2_create_inode - Create a new inode
  466. * @dir: The parent directory
  467. * @dentry: The new dentry
  468. * @mode: The permissions on the new inode
  469. * @dev: For device nodes, this is the device number
  470. * @symname: For symlinks, this is the link destination
  471. * @size: The initial size of the inode (ignored for directories)
  472. *
  473. * Returns: 0 on success, or error code
  474. */
  475. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  476. umode_t mode, dev_t dev, const char *symname,
  477. unsigned int size, int excl)
  478. {
  479. const struct qstr *name = &dentry->d_name;
  480. struct gfs2_holder ghs[2];
  481. struct inode *inode = NULL;
  482. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  483. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  484. struct gfs2_glock *io_gl;
  485. int error;
  486. u32 aflags = 0;
  487. int arq;
  488. if (!name->len || name->len > GFS2_FNAMESIZE)
  489. return -ENAMETOOLONG;
  490. error = gfs2_rs_alloc(dip);
  491. if (error)
  492. return error;
  493. error = gfs2_rindex_update(sdp);
  494. if (error)
  495. return error;
  496. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  497. if (error)
  498. goto fail;
  499. error = create_ok(dip, name, mode);
  500. if ((error == -EEXIST) && S_ISREG(mode) && !excl) {
  501. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  502. gfs2_glock_dq_uninit(ghs);
  503. d_instantiate(dentry, inode);
  504. return IS_ERR(inode) ? PTR_ERR(inode) : 0;
  505. }
  506. if (error)
  507. goto fail_gunlock;
  508. arq = error = gfs2_diradd_alloc_required(dir, name);
  509. if (error < 0)
  510. goto fail_gunlock;
  511. inode = new_inode(sdp->sd_vfs);
  512. error = -ENOMEM;
  513. if (!inode)
  514. goto fail_gunlock;
  515. ip = GFS2_I(inode);
  516. error = gfs2_rs_alloc(ip);
  517. if (error)
  518. goto fail_free_inode;
  519. inode->i_mode = mode;
  520. set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
  521. inode->i_rdev = dev;
  522. inode->i_size = size;
  523. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  524. gfs2_set_inode_blocks(inode, 1);
  525. munge_mode_uid_gid(dip, inode);
  526. ip->i_goal = dip->i_goal;
  527. ip->i_diskflags = 0;
  528. ip->i_eattr = 0;
  529. ip->i_height = 0;
  530. ip->i_depth = 0;
  531. ip->i_entries = 0;
  532. switch(mode & S_IFMT) {
  533. case S_IFREG:
  534. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  535. gfs2_tune_get(sdp, gt_new_files_jdata))
  536. ip->i_diskflags |= GFS2_DIF_JDATA;
  537. gfs2_set_aops(inode);
  538. break;
  539. case S_IFDIR:
  540. ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
  541. ip->i_diskflags |= GFS2_DIF_JDATA;
  542. ip->i_entries = 2;
  543. break;
  544. }
  545. gfs2_set_inode_flags(inode);
  546. if ((GFS2_I(sdp->sd_root_dir->d_inode) == dip) ||
  547. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  548. aflags |= GFS2_AF_ORLOV;
  549. error = alloc_dinode(ip, aflags);
  550. if (error)
  551. goto fail_free_inode;
  552. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  553. if (error)
  554. goto fail_free_inode;
  555. ip->i_gl->gl_object = ip;
  556. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  557. if (error)
  558. goto fail_free_inode;
  559. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  560. if (error)
  561. goto fail_gunlock2;
  562. init_dinode(dip, ip, symname);
  563. gfs2_trans_end(sdp);
  564. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  565. if (error)
  566. goto fail_gunlock2;
  567. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  568. if (error)
  569. goto fail_gunlock2;
  570. ip->i_iopen_gh.gh_gl->gl_object = ip;
  571. gfs2_glock_put(io_gl);
  572. gfs2_set_iop(inode);
  573. insert_inode_hash(inode);
  574. error = gfs2_acl_create(dip, inode);
  575. if (error)
  576. goto fail_gunlock3;
  577. error = gfs2_security_init(dip, ip, name);
  578. if (error)
  579. goto fail_gunlock3;
  580. error = link_dinode(dip, name, ip, arq);
  581. if (error)
  582. goto fail_gunlock3;
  583. mark_inode_dirty(inode);
  584. gfs2_glock_dq_uninit(ghs);
  585. gfs2_glock_dq_uninit(ghs + 1);
  586. d_instantiate(dentry, inode);
  587. return 0;
  588. fail_gunlock3:
  589. gfs2_glock_dq_uninit(ghs + 1);
  590. if (ip->i_gl)
  591. gfs2_glock_put(ip->i_gl);
  592. goto fail_gunlock;
  593. fail_gunlock2:
  594. gfs2_glock_dq_uninit(ghs + 1);
  595. fail_free_inode:
  596. if (ip->i_gl)
  597. gfs2_glock_put(ip->i_gl);
  598. gfs2_rs_delete(ip);
  599. free_inode_nonrcu(inode);
  600. inode = NULL;
  601. fail_gunlock:
  602. gfs2_glock_dq_uninit(ghs);
  603. if (inode && !IS_ERR(inode)) {
  604. clear_nlink(inode);
  605. mark_inode_dirty(inode);
  606. set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
  607. iput(inode);
  608. }
  609. fail:
  610. return error;
  611. }
  612. /**
  613. * gfs2_create - Create a file
  614. * @dir: The directory in which to create the file
  615. * @dentry: The dentry of the new file
  616. * @mode: The mode of the new file
  617. *
  618. * Returns: errno
  619. */
  620. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  621. umode_t mode, bool excl)
  622. {
  623. return gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0, excl);
  624. }
  625. /**
  626. * gfs2_lookup - Look up a filename in a directory and return its inode
  627. * @dir: The directory inode
  628. * @dentry: The dentry of the new inode
  629. * @nd: passed from Linux VFS, ignored by us
  630. *
  631. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  632. *
  633. * Returns: errno
  634. */
  635. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  636. unsigned int flags)
  637. {
  638. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  639. if (inode && !IS_ERR(inode)) {
  640. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  641. struct gfs2_holder gh;
  642. int error;
  643. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  644. if (error) {
  645. iput(inode);
  646. return ERR_PTR(error);
  647. }
  648. gfs2_glock_dq_uninit(&gh);
  649. }
  650. return d_splice_alias(inode, dentry);
  651. }
  652. /**
  653. * gfs2_link - Link to a file
  654. * @old_dentry: The inode to link
  655. * @dir: Add link to this directory
  656. * @dentry: The name of the link
  657. *
  658. * Link the inode in "old_dentry" into the directory "dir" with the
  659. * name in "dentry".
  660. *
  661. * Returns: errno
  662. */
  663. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  664. struct dentry *dentry)
  665. {
  666. struct gfs2_inode *dip = GFS2_I(dir);
  667. struct gfs2_sbd *sdp = GFS2_SB(dir);
  668. struct inode *inode = old_dentry->d_inode;
  669. struct gfs2_inode *ip = GFS2_I(inode);
  670. struct gfs2_holder ghs[2];
  671. struct buffer_head *dibh;
  672. int alloc_required;
  673. int error;
  674. if (S_ISDIR(inode->i_mode))
  675. return -EPERM;
  676. error = gfs2_rs_alloc(dip);
  677. if (error)
  678. return error;
  679. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  680. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  681. error = gfs2_glock_nq(ghs); /* parent */
  682. if (error)
  683. goto out_parent;
  684. error = gfs2_glock_nq(ghs + 1); /* child */
  685. if (error)
  686. goto out_child;
  687. error = -ENOENT;
  688. if (inode->i_nlink == 0)
  689. goto out_gunlock;
  690. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  691. if (error)
  692. goto out_gunlock;
  693. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  694. switch (error) {
  695. case -ENOENT:
  696. break;
  697. case 0:
  698. error = -EEXIST;
  699. default:
  700. goto out_gunlock;
  701. }
  702. error = -EINVAL;
  703. if (!dip->i_inode.i_nlink)
  704. goto out_gunlock;
  705. error = -EFBIG;
  706. if (dip->i_entries == (u32)-1)
  707. goto out_gunlock;
  708. error = -EPERM;
  709. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  710. goto out_gunlock;
  711. error = -EINVAL;
  712. if (!ip->i_inode.i_nlink)
  713. goto out_gunlock;
  714. error = -EMLINK;
  715. if (ip->i_inode.i_nlink == (u32)-1)
  716. goto out_gunlock;
  717. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  718. if (error < 0)
  719. goto out_gunlock;
  720. error = 0;
  721. if (alloc_required) {
  722. error = gfs2_quota_lock_check(dip);
  723. if (error)
  724. goto out_gunlock;
  725. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  726. if (error)
  727. goto out_gunlock_q;
  728. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  729. gfs2_rg_blocks(dip, sdp->sd_max_dirres) +
  730. 2 * RES_DINODE + RES_STATFS +
  731. RES_QUOTA, 0);
  732. if (error)
  733. goto out_ipres;
  734. } else {
  735. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  736. if (error)
  737. goto out_ipres;
  738. }
  739. error = gfs2_meta_inode_buffer(ip, &dibh);
  740. if (error)
  741. goto out_end_trans;
  742. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  743. if (error)
  744. goto out_brelse;
  745. gfs2_trans_add_meta(ip->i_gl, dibh);
  746. inc_nlink(&ip->i_inode);
  747. ip->i_inode.i_ctime = CURRENT_TIME;
  748. ihold(inode);
  749. d_instantiate(dentry, inode);
  750. mark_inode_dirty(inode);
  751. out_brelse:
  752. brelse(dibh);
  753. out_end_trans:
  754. gfs2_trans_end(sdp);
  755. out_ipres:
  756. if (alloc_required)
  757. gfs2_inplace_release(dip);
  758. out_gunlock_q:
  759. if (alloc_required)
  760. gfs2_quota_unlock(dip);
  761. out_gunlock:
  762. gfs2_glock_dq(ghs + 1);
  763. out_child:
  764. gfs2_glock_dq(ghs);
  765. out_parent:
  766. gfs2_holder_uninit(ghs);
  767. gfs2_holder_uninit(ghs + 1);
  768. return error;
  769. }
  770. /*
  771. * gfs2_unlink_ok - check to see that a inode is still in a directory
  772. * @dip: the directory
  773. * @name: the name of the file
  774. * @ip: the inode
  775. *
  776. * Assumes that the lock on (at least) @dip is held.
  777. *
  778. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  779. */
  780. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  781. const struct gfs2_inode *ip)
  782. {
  783. int error;
  784. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  785. return -EPERM;
  786. if ((dip->i_inode.i_mode & S_ISVTX) &&
  787. !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
  788. !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
  789. return -EPERM;
  790. if (IS_APPEND(&dip->i_inode))
  791. return -EPERM;
  792. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  793. if (error)
  794. return error;
  795. error = gfs2_dir_check(&dip->i_inode, name, ip);
  796. if (error)
  797. return error;
  798. return 0;
  799. }
  800. /**
  801. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  802. * @dip: The parent directory
  803. * @name: The name of the entry in the parent directory
  804. * @inode: The inode to be removed
  805. *
  806. * Called with all the locks and in a transaction. This will only be
  807. * called for a directory after it has been checked to ensure it is empty.
  808. *
  809. * Returns: 0 on success, or an error
  810. */
  811. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  812. const struct dentry *dentry)
  813. {
  814. struct inode *inode = dentry->d_inode;
  815. struct gfs2_inode *ip = GFS2_I(inode);
  816. int error;
  817. error = gfs2_dir_del(dip, dentry);
  818. if (error)
  819. return error;
  820. ip->i_entries = 0;
  821. inode->i_ctime = CURRENT_TIME;
  822. if (S_ISDIR(inode->i_mode))
  823. clear_nlink(inode);
  824. else
  825. drop_nlink(inode);
  826. mark_inode_dirty(inode);
  827. if (inode->i_nlink == 0)
  828. gfs2_unlink_di(inode);
  829. return 0;
  830. }
  831. /**
  832. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  833. * @dir: The inode of the directory containing the inode to unlink
  834. * @dentry: The file itself
  835. *
  836. * This routine uses the type of the inode as a flag to figure out
  837. * whether this is an unlink or an rmdir.
  838. *
  839. * Returns: errno
  840. */
  841. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  842. {
  843. struct gfs2_inode *dip = GFS2_I(dir);
  844. struct gfs2_sbd *sdp = GFS2_SB(dir);
  845. struct inode *inode = dentry->d_inode;
  846. struct gfs2_inode *ip = GFS2_I(inode);
  847. struct gfs2_holder ghs[3];
  848. struct gfs2_rgrpd *rgd;
  849. int error;
  850. error = gfs2_rindex_update(sdp);
  851. if (error)
  852. return error;
  853. error = -EROFS;
  854. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  855. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  856. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  857. if (!rgd)
  858. goto out_inodes;
  859. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  860. error = gfs2_glock_nq(ghs); /* parent */
  861. if (error)
  862. goto out_parent;
  863. error = gfs2_glock_nq(ghs + 1); /* child */
  864. if (error)
  865. goto out_child;
  866. error = -ENOENT;
  867. if (inode->i_nlink == 0)
  868. goto out_rgrp;
  869. if (S_ISDIR(inode->i_mode)) {
  870. error = -ENOTEMPTY;
  871. if (ip->i_entries > 2 || inode->i_nlink > 2)
  872. goto out_rgrp;
  873. }
  874. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  875. if (error)
  876. goto out_rgrp;
  877. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  878. if (error)
  879. goto out_gunlock;
  880. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  881. if (error)
  882. goto out_end_trans;
  883. error = gfs2_unlink_inode(dip, dentry);
  884. out_end_trans:
  885. gfs2_trans_end(sdp);
  886. out_gunlock:
  887. gfs2_glock_dq(ghs + 2);
  888. out_rgrp:
  889. gfs2_glock_dq(ghs + 1);
  890. out_child:
  891. gfs2_glock_dq(ghs);
  892. out_parent:
  893. gfs2_holder_uninit(ghs + 2);
  894. out_inodes:
  895. gfs2_holder_uninit(ghs + 1);
  896. gfs2_holder_uninit(ghs);
  897. return error;
  898. }
  899. /**
  900. * gfs2_symlink - Create a symlink
  901. * @dir: The directory to create the symlink in
  902. * @dentry: The dentry to put the symlink in
  903. * @symname: The thing which the link points to
  904. *
  905. * Returns: errno
  906. */
  907. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  908. const char *symname)
  909. {
  910. struct gfs2_sbd *sdp = GFS2_SB(dir);
  911. unsigned int size;
  912. size = strlen(symname);
  913. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  914. return -ENAMETOOLONG;
  915. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  916. }
  917. /**
  918. * gfs2_mkdir - Make a directory
  919. * @dir: The parent directory of the new one
  920. * @dentry: The dentry of the new directory
  921. * @mode: The mode of the new directory
  922. *
  923. * Returns: errno
  924. */
  925. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  926. {
  927. struct gfs2_sbd *sdp = GFS2_SB(dir);
  928. unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  929. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, dsize, 0);
  930. }
  931. /**
  932. * gfs2_mknod - Make a special file
  933. * @dir: The directory in which the special file will reside
  934. * @dentry: The dentry of the special file
  935. * @mode: The mode of the special file
  936. * @dev: The device specification of the special file
  937. *
  938. */
  939. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  940. dev_t dev)
  941. {
  942. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0, 0);
  943. }
  944. /*
  945. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  946. * @this: move this
  947. * @to: to here
  948. *
  949. * Follow @to back to the root and make sure we don't encounter @this
  950. * Assumes we already hold the rename lock.
  951. *
  952. * Returns: errno
  953. */
  954. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  955. {
  956. struct inode *dir = &to->i_inode;
  957. struct super_block *sb = dir->i_sb;
  958. struct inode *tmp;
  959. int error = 0;
  960. igrab(dir);
  961. for (;;) {
  962. if (dir == &this->i_inode) {
  963. error = -EINVAL;
  964. break;
  965. }
  966. if (dir == sb->s_root->d_inode) {
  967. error = 0;
  968. break;
  969. }
  970. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  971. if (IS_ERR(tmp)) {
  972. error = PTR_ERR(tmp);
  973. break;
  974. }
  975. iput(dir);
  976. dir = tmp;
  977. }
  978. iput(dir);
  979. return error;
  980. }
  981. /**
  982. * gfs2_rename - Rename a file
  983. * @odir: Parent directory of old file name
  984. * @odentry: The old dentry of the file
  985. * @ndir: Parent directory of new file name
  986. * @ndentry: The new dentry of the file
  987. *
  988. * Returns: errno
  989. */
  990. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  991. struct inode *ndir, struct dentry *ndentry)
  992. {
  993. struct gfs2_inode *odip = GFS2_I(odir);
  994. struct gfs2_inode *ndip = GFS2_I(ndir);
  995. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  996. struct gfs2_inode *nip = NULL;
  997. struct gfs2_sbd *sdp = GFS2_SB(odir);
  998. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  999. struct gfs2_rgrpd *nrgd;
  1000. unsigned int num_gh;
  1001. int dir_rename = 0;
  1002. int alloc_required = 0;
  1003. unsigned int x;
  1004. int error;
  1005. if (ndentry->d_inode) {
  1006. nip = GFS2_I(ndentry->d_inode);
  1007. if (ip == nip)
  1008. return 0;
  1009. }
  1010. error = gfs2_rindex_update(sdp);
  1011. if (error)
  1012. return error;
  1013. error = gfs2_rs_alloc(ndip);
  1014. if (error)
  1015. return error;
  1016. if (odip != ndip) {
  1017. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1018. 0, &r_gh);
  1019. if (error)
  1020. goto out;
  1021. if (S_ISDIR(ip->i_inode.i_mode)) {
  1022. dir_rename = 1;
  1023. /* don't move a dirctory into it's subdir */
  1024. error = gfs2_ok_to_move(ip, ndip);
  1025. if (error)
  1026. goto out_gunlock_r;
  1027. }
  1028. }
  1029. num_gh = 1;
  1030. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1031. if (odip != ndip) {
  1032. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1033. num_gh++;
  1034. }
  1035. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1036. num_gh++;
  1037. if (nip) {
  1038. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1039. num_gh++;
  1040. /* grab the resource lock for unlink flag twiddling
  1041. * this is the case of the target file already existing
  1042. * so we unlink before doing the rename
  1043. */
  1044. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1045. if (nrgd)
  1046. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1047. }
  1048. for (x = 0; x < num_gh; x++) {
  1049. error = gfs2_glock_nq(ghs + x);
  1050. if (error)
  1051. goto out_gunlock;
  1052. }
  1053. error = -ENOENT;
  1054. if (ip->i_inode.i_nlink == 0)
  1055. goto out_gunlock;
  1056. /* Check out the old directory */
  1057. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1058. if (error)
  1059. goto out_gunlock;
  1060. /* Check out the new directory */
  1061. if (nip) {
  1062. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1063. if (error)
  1064. goto out_gunlock;
  1065. if (nip->i_inode.i_nlink == 0) {
  1066. error = -EAGAIN;
  1067. goto out_gunlock;
  1068. }
  1069. if (S_ISDIR(nip->i_inode.i_mode)) {
  1070. if (nip->i_entries < 2) {
  1071. gfs2_consist_inode(nip);
  1072. error = -EIO;
  1073. goto out_gunlock;
  1074. }
  1075. if (nip->i_entries > 2) {
  1076. error = -ENOTEMPTY;
  1077. goto out_gunlock;
  1078. }
  1079. }
  1080. } else {
  1081. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1082. if (error)
  1083. goto out_gunlock;
  1084. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1085. switch (error) {
  1086. case -ENOENT:
  1087. error = 0;
  1088. break;
  1089. case 0:
  1090. error = -EEXIST;
  1091. default:
  1092. goto out_gunlock;
  1093. };
  1094. if (odip != ndip) {
  1095. if (!ndip->i_inode.i_nlink) {
  1096. error = -ENOENT;
  1097. goto out_gunlock;
  1098. }
  1099. if (ndip->i_entries == (u32)-1) {
  1100. error = -EFBIG;
  1101. goto out_gunlock;
  1102. }
  1103. if (S_ISDIR(ip->i_inode.i_mode) &&
  1104. ndip->i_inode.i_nlink == (u32)-1) {
  1105. error = -EMLINK;
  1106. goto out_gunlock;
  1107. }
  1108. }
  1109. }
  1110. /* Check out the dir to be renamed */
  1111. if (dir_rename) {
  1112. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1113. if (error)
  1114. goto out_gunlock;
  1115. }
  1116. if (nip == NULL)
  1117. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1118. error = alloc_required;
  1119. if (error < 0)
  1120. goto out_gunlock;
  1121. if (alloc_required) {
  1122. error = gfs2_quota_lock_check(ndip);
  1123. if (error)
  1124. goto out_gunlock;
  1125. error = gfs2_inplace_reserve(ndip, sdp->sd_max_dirres, 0);
  1126. if (error)
  1127. goto out_gunlock_q;
  1128. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1129. gfs2_rg_blocks(ndip, sdp->sd_max_dirres) +
  1130. 4 * RES_DINODE + 4 * RES_LEAF +
  1131. RES_STATFS + RES_QUOTA + 4, 0);
  1132. if (error)
  1133. goto out_ipreserv;
  1134. } else {
  1135. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1136. 5 * RES_LEAF + 4, 0);
  1137. if (error)
  1138. goto out_gunlock;
  1139. }
  1140. /* Remove the target file, if it exists */
  1141. if (nip)
  1142. error = gfs2_unlink_inode(ndip, ndentry);
  1143. if (dir_rename) {
  1144. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1145. if (error)
  1146. goto out_end_trans;
  1147. } else {
  1148. struct buffer_head *dibh;
  1149. error = gfs2_meta_inode_buffer(ip, &dibh);
  1150. if (error)
  1151. goto out_end_trans;
  1152. ip->i_inode.i_ctime = CURRENT_TIME;
  1153. gfs2_trans_add_meta(ip->i_gl, dibh);
  1154. gfs2_dinode_out(ip, dibh->b_data);
  1155. brelse(dibh);
  1156. }
  1157. error = gfs2_dir_del(odip, odentry);
  1158. if (error)
  1159. goto out_end_trans;
  1160. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1161. if (error)
  1162. goto out_end_trans;
  1163. out_end_trans:
  1164. gfs2_trans_end(sdp);
  1165. out_ipreserv:
  1166. if (alloc_required)
  1167. gfs2_inplace_release(ndip);
  1168. out_gunlock_q:
  1169. if (alloc_required)
  1170. gfs2_quota_unlock(ndip);
  1171. out_gunlock:
  1172. while (x--) {
  1173. gfs2_glock_dq(ghs + x);
  1174. gfs2_holder_uninit(ghs + x);
  1175. }
  1176. out_gunlock_r:
  1177. if (r_gh.gh_gl)
  1178. gfs2_glock_dq_uninit(&r_gh);
  1179. out:
  1180. return error;
  1181. }
  1182. /**
  1183. * gfs2_follow_link - Follow a symbolic link
  1184. * @dentry: The dentry of the link
  1185. * @nd: Data that we pass to vfs_follow_link()
  1186. *
  1187. * This can handle symlinks of any size.
  1188. *
  1189. * Returns: 0 on success or error code
  1190. */
  1191. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1192. {
  1193. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1194. struct gfs2_holder i_gh;
  1195. struct buffer_head *dibh;
  1196. unsigned int size;
  1197. char *buf;
  1198. int error;
  1199. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1200. error = gfs2_glock_nq(&i_gh);
  1201. if (error) {
  1202. gfs2_holder_uninit(&i_gh);
  1203. nd_set_link(nd, ERR_PTR(error));
  1204. return NULL;
  1205. }
  1206. size = (unsigned int)i_size_read(&ip->i_inode);
  1207. if (size == 0) {
  1208. gfs2_consist_inode(ip);
  1209. buf = ERR_PTR(-EIO);
  1210. goto out;
  1211. }
  1212. error = gfs2_meta_inode_buffer(ip, &dibh);
  1213. if (error) {
  1214. buf = ERR_PTR(error);
  1215. goto out;
  1216. }
  1217. buf = kzalloc(size + 1, GFP_NOFS);
  1218. if (!buf)
  1219. buf = ERR_PTR(-ENOMEM);
  1220. else
  1221. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1222. brelse(dibh);
  1223. out:
  1224. gfs2_glock_dq_uninit(&i_gh);
  1225. nd_set_link(nd, buf);
  1226. return NULL;
  1227. }
  1228. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1229. {
  1230. char *s = nd_get_link(nd);
  1231. if (!IS_ERR(s))
  1232. kfree(s);
  1233. }
  1234. /**
  1235. * gfs2_permission -
  1236. * @inode: The inode
  1237. * @mask: The mask to be tested
  1238. * @flags: Indicates whether this is an RCU path walk or not
  1239. *
  1240. * This may be called from the VFS directly, or from within GFS2 with the
  1241. * inode locked, so we look to see if the glock is already locked and only
  1242. * lock the glock if its not already been done.
  1243. *
  1244. * Returns: errno
  1245. */
  1246. int gfs2_permission(struct inode *inode, int mask)
  1247. {
  1248. struct gfs2_inode *ip;
  1249. struct gfs2_holder i_gh;
  1250. int error;
  1251. int unlock = 0;
  1252. ip = GFS2_I(inode);
  1253. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1254. if (mask & MAY_NOT_BLOCK)
  1255. return -ECHILD;
  1256. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1257. if (error)
  1258. return error;
  1259. unlock = 1;
  1260. }
  1261. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1262. error = -EACCES;
  1263. else
  1264. error = generic_permission(inode, mask);
  1265. if (unlock)
  1266. gfs2_glock_dq_uninit(&i_gh);
  1267. return error;
  1268. }
  1269. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1270. {
  1271. setattr_copy(inode, attr);
  1272. mark_inode_dirty(inode);
  1273. return 0;
  1274. }
  1275. /**
  1276. * gfs2_setattr_simple -
  1277. * @ip:
  1278. * @attr:
  1279. *
  1280. * Returns: errno
  1281. */
  1282. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1283. {
  1284. int error;
  1285. if (current->journal_info)
  1286. return __gfs2_setattr_simple(inode, attr);
  1287. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1288. if (error)
  1289. return error;
  1290. error = __gfs2_setattr_simple(inode, attr);
  1291. gfs2_trans_end(GFS2_SB(inode));
  1292. return error;
  1293. }
  1294. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1295. {
  1296. struct gfs2_inode *ip = GFS2_I(inode);
  1297. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1298. kuid_t ouid, nuid;
  1299. kgid_t ogid, ngid;
  1300. int error;
  1301. ouid = inode->i_uid;
  1302. ogid = inode->i_gid;
  1303. nuid = attr->ia_uid;
  1304. ngid = attr->ia_gid;
  1305. if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
  1306. ouid = nuid = NO_UID_QUOTA_CHANGE;
  1307. if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
  1308. ogid = ngid = NO_GID_QUOTA_CHANGE;
  1309. error = gfs2_quota_lock(ip, nuid, ngid);
  1310. if (error)
  1311. return error;
  1312. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1313. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1314. error = gfs2_quota_check(ip, nuid, ngid);
  1315. if (error)
  1316. goto out_gunlock_q;
  1317. }
  1318. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1319. if (error)
  1320. goto out_gunlock_q;
  1321. error = gfs2_setattr_simple(inode, attr);
  1322. if (error)
  1323. goto out_end_trans;
  1324. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1325. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1326. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1327. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1328. gfs2_quota_change(ip, blocks, nuid, ngid);
  1329. }
  1330. out_end_trans:
  1331. gfs2_trans_end(sdp);
  1332. out_gunlock_q:
  1333. gfs2_quota_unlock(ip);
  1334. return error;
  1335. }
  1336. /**
  1337. * gfs2_setattr - Change attributes on an inode
  1338. * @dentry: The dentry which is changing
  1339. * @attr: The structure describing the change
  1340. *
  1341. * The VFS layer wants to change one or more of an inodes attributes. Write
  1342. * that change out to disk.
  1343. *
  1344. * Returns: errno
  1345. */
  1346. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1347. {
  1348. struct inode *inode = dentry->d_inode;
  1349. struct gfs2_inode *ip = GFS2_I(inode);
  1350. struct gfs2_holder i_gh;
  1351. int error;
  1352. error = gfs2_rs_alloc(ip);
  1353. if (error)
  1354. return error;
  1355. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1356. if (error)
  1357. return error;
  1358. error = -EPERM;
  1359. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1360. goto out;
  1361. error = inode_change_ok(inode, attr);
  1362. if (error)
  1363. goto out;
  1364. if (attr->ia_valid & ATTR_SIZE)
  1365. error = gfs2_setattr_size(inode, attr->ia_size);
  1366. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1367. error = setattr_chown(inode, attr);
  1368. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1369. error = gfs2_acl_chmod(ip, attr);
  1370. else
  1371. error = gfs2_setattr_simple(inode, attr);
  1372. out:
  1373. if (!error)
  1374. mark_inode_dirty(inode);
  1375. gfs2_glock_dq_uninit(&i_gh);
  1376. return error;
  1377. }
  1378. /**
  1379. * gfs2_getattr - Read out an inode's attributes
  1380. * @mnt: The vfsmount the inode is being accessed from
  1381. * @dentry: The dentry to stat
  1382. * @stat: The inode's stats
  1383. *
  1384. * This may be called from the VFS directly, or from within GFS2 with the
  1385. * inode locked, so we look to see if the glock is already locked and only
  1386. * lock the glock if its not already been done. Note that its the NFS
  1387. * readdirplus operation which causes this to be called (from filldir)
  1388. * with the glock already held.
  1389. *
  1390. * Returns: errno
  1391. */
  1392. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1393. struct kstat *stat)
  1394. {
  1395. struct inode *inode = dentry->d_inode;
  1396. struct gfs2_inode *ip = GFS2_I(inode);
  1397. struct gfs2_holder gh;
  1398. int error;
  1399. int unlock = 0;
  1400. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1401. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1402. if (error)
  1403. return error;
  1404. unlock = 1;
  1405. }
  1406. generic_fillattr(inode, stat);
  1407. if (unlock)
  1408. gfs2_glock_dq_uninit(&gh);
  1409. return 0;
  1410. }
  1411. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1412. const void *data, size_t size, int flags)
  1413. {
  1414. struct inode *inode = dentry->d_inode;
  1415. struct gfs2_inode *ip = GFS2_I(inode);
  1416. struct gfs2_holder gh;
  1417. int ret;
  1418. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1419. ret = gfs2_glock_nq(&gh);
  1420. if (ret == 0) {
  1421. ret = gfs2_rs_alloc(ip);
  1422. if (ret == 0)
  1423. ret = generic_setxattr(dentry, name, data, size, flags);
  1424. gfs2_glock_dq(&gh);
  1425. }
  1426. gfs2_holder_uninit(&gh);
  1427. return ret;
  1428. }
  1429. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1430. void *data, size_t size)
  1431. {
  1432. struct inode *inode = dentry->d_inode;
  1433. struct gfs2_inode *ip = GFS2_I(inode);
  1434. struct gfs2_holder gh;
  1435. int ret;
  1436. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1437. ret = gfs2_glock_nq(&gh);
  1438. if (ret == 0) {
  1439. ret = generic_getxattr(dentry, name, data, size);
  1440. gfs2_glock_dq(&gh);
  1441. }
  1442. gfs2_holder_uninit(&gh);
  1443. return ret;
  1444. }
  1445. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1446. {
  1447. struct inode *inode = dentry->d_inode;
  1448. struct gfs2_inode *ip = GFS2_I(inode);
  1449. struct gfs2_holder gh;
  1450. int ret;
  1451. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1452. ret = gfs2_glock_nq(&gh);
  1453. if (ret == 0) {
  1454. ret = gfs2_rs_alloc(ip);
  1455. if (ret == 0)
  1456. ret = generic_removexattr(dentry, name);
  1457. gfs2_glock_dq(&gh);
  1458. }
  1459. gfs2_holder_uninit(&gh);
  1460. return ret;
  1461. }
  1462. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1463. u64 start, u64 len)
  1464. {
  1465. struct gfs2_inode *ip = GFS2_I(inode);
  1466. struct gfs2_holder gh;
  1467. int ret;
  1468. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1469. if (ret)
  1470. return ret;
  1471. mutex_lock(&inode->i_mutex);
  1472. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1473. if (ret)
  1474. goto out;
  1475. if (gfs2_is_stuffed(ip)) {
  1476. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1477. u64 size = i_size_read(inode);
  1478. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1479. FIEMAP_EXTENT_DATA_INLINE;
  1480. phys += sizeof(struct gfs2_dinode);
  1481. phys += start;
  1482. if (start + len > size)
  1483. len = size - start;
  1484. if (start < size)
  1485. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1486. len, flags);
  1487. if (ret == 1)
  1488. ret = 0;
  1489. } else {
  1490. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1491. gfs2_block_map);
  1492. }
  1493. gfs2_glock_dq_uninit(&gh);
  1494. out:
  1495. mutex_unlock(&inode->i_mutex);
  1496. return ret;
  1497. }
  1498. const struct inode_operations gfs2_file_iops = {
  1499. .permission = gfs2_permission,
  1500. .setattr = gfs2_setattr,
  1501. .getattr = gfs2_getattr,
  1502. .setxattr = gfs2_setxattr,
  1503. .getxattr = gfs2_getxattr,
  1504. .listxattr = gfs2_listxattr,
  1505. .removexattr = gfs2_removexattr,
  1506. .fiemap = gfs2_fiemap,
  1507. .get_acl = gfs2_get_acl,
  1508. };
  1509. const struct inode_operations gfs2_dir_iops = {
  1510. .create = gfs2_create,
  1511. .lookup = gfs2_lookup,
  1512. .link = gfs2_link,
  1513. .unlink = gfs2_unlink,
  1514. .symlink = gfs2_symlink,
  1515. .mkdir = gfs2_mkdir,
  1516. .rmdir = gfs2_unlink,
  1517. .mknod = gfs2_mknod,
  1518. .rename = gfs2_rename,
  1519. .permission = gfs2_permission,
  1520. .setattr = gfs2_setattr,
  1521. .getattr = gfs2_getattr,
  1522. .setxattr = gfs2_setxattr,
  1523. .getxattr = gfs2_getxattr,
  1524. .listxattr = gfs2_listxattr,
  1525. .removexattr = gfs2_removexattr,
  1526. .fiemap = gfs2_fiemap,
  1527. .get_acl = gfs2_get_acl,
  1528. };
  1529. const struct inode_operations gfs2_symlink_iops = {
  1530. .readlink = generic_readlink,
  1531. .follow_link = gfs2_follow_link,
  1532. .put_link = gfs2_put_link,
  1533. .permission = gfs2_permission,
  1534. .setattr = gfs2_setattr,
  1535. .getattr = gfs2_getattr,
  1536. .setxattr = gfs2_setxattr,
  1537. .getxattr = gfs2_getxattr,
  1538. .listxattr = gfs2_listxattr,
  1539. .removexattr = gfs2_removexattr,
  1540. .fiemap = gfs2_fiemap,
  1541. .get_acl = gfs2_get_acl,
  1542. };